typedef int SltDatatype;
typedef struct Stack
{
SltDatatype* a;
int top;
int capacity;
} ST;
void STInit ( ST* ps) ;
void STDestroy ( ST* ps) ;
void STPush ( ST* ps, SltDatatype x) ;
void STPop ( ST* ps) ;
SltDatatype STTop ( ps) ;
int Size ( ps) ;
bool STEmpty ( ST* ps) ;
void STInit ( ST* ps)
{
assert ( ps) ;
ps-> a = NULL ;
ps-> capacity = 0 ;
ps-> top = 0 ;
}
void STPush ( ST* ps, SltDatatype x)
{
assert ( ps) ;
if ( ps-> top == ps-> capacity)
{
int newcapacity = ps-> capacity == 0 ? 4 : ps-> capacity * 2 ;
SltDatatype * tmp= ( SltDatatype* ) realloc ( ps-> a, sizeof ( SltDatatype) * newcapacity) ;
if ( tmp == NULL )
{
perror ( "realloc fail" ) ;
exit ( - 1 ) ;
}
ps -> a = tmp;
ps-> capacity = newcapacity;
}
ps-> a[ ps-> top] = x;
ps-> top++ ;
}
void STDestroy ( ST* ps)
{
assert ( ps) ;
free ( ps-> a) ;
ps-> a = NULL ;
ps-> capacity = 0 ;
ps-> top = 0 ;
}
void STPop ( ST* ps)
{
assert ( ps) ;
assert ( ps-> top> 0 ) ;
ps-> top-- ;
}
SltDatatype STTop ( ST* ps)
{
assert ( ps) ;
assert ( ps-> top > 0 ) ;
return ps-> a[ ps-> top - 1 ] ;
}
int Size ( ST* ps)
{
assert ( ps) ;
return ps-> top;
}
bool STEmpty ( ST* ps)
{
assert ( ps) ;
return ps-> top == 0 ;
}
typedef struct {
ST pushst;
ST popst;
} MyQueue;
MyQueue* myQueueCreate ( ) {
MyQueue* obj= ( MyQueue* ) malloc ( sizeof ( MyQueue) ) ;
STInit ( & obj-> pushst) ;
STInit ( & obj-> popst) ;
return obj;
}
void myQueuePush ( MyQueue* obj, int x) {
STPush ( & obj-> pushst, x) ;
}
int myQueuePeek ( MyQueue* obj) {
if ( STEmpty ( & obj-> popst) )
{
while ( ! STEmpty ( & obj-> pushst) )
{
STPush ( & obj-> popst, STTop ( & obj-> pushst) ) ;
STPop ( & obj-> pushst) ;
}
}
return STTop ( & obj-> popst) ;
}
int myQueuePop ( MyQueue* obj) {
int val= myQueuePeek ( obj) ;
STPop ( & obj-> popst) ;
return val;
}
bool myQueueEmpty ( MyQueue* obj) {
return STEmpty ( & obj-> popst) && STEmpty ( & obj-> pushst) ;
}
void myQueueFree ( MyQueue* obj) {
STDestroy ( & obj-> popst) ;
STDestroy ( & obj-> pushst) ;
free ( obj) ;
}