#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}Stack;
void StackInit(Stack* ps)
{
ps->capacity = 5;
ps->a = (STDataType*)malloc(sizeof(STDataType)*ps->capacity);
if (!ps->a)
{
assert(0);
}
ps->top = 0;
}
void CheckCapacity(Stack *ps)
{
if (ps->top >= ps->capacity)
{
ps->capacity = 2 * ps->capacity;
ps->a = (STDataType*)realloc(ps->a,sizeof(STDataType)*ps->capacity);
if (!ps->a)
{
assert(0);
}
}
}
bool StackEmpty(Stack* ps)
{
return 0 == ps->top;
}
bool StackFull(Stack *ps)
{
return ps->top==ps->capacity;
}
void StackPush(Stack* ps, STDataType data)
{
CheckCapacity(ps);
ps->a[ps->top] = data;
ps->top++;
}
void StackPop(Stack* ps)
{
if (StackEmpty(ps))
return;
ps->top--;
}
STDataType StackTop(Stack* ps)
{
if (StackEmpty(ps))
{
return (STDataType)0;
}
return ps->a[ps->top - 1];
}
int StackSize(Stack* ps)
{
return ps->top;
}
void StackDestroy(Stack* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
typedef struct
{
Stack s1;
Stack s2;
} MyQueue;
MyQueue* myQueueCreate()
{
MyQueue *obj=(MyQueue*)malloc(sizeof(MyQueue));
if(!obj)
{
assert(0);
return NULL;
}
StackInit(&obj->s1);
StackInit(&obj->s2);
return obj;
}
void myQueuePush(MyQueue* obj, int x)
{
if(StackFull(&obj->s1))
{
return;
}
else
{
StackPush(&obj->s1,x);
}
}
int myQueuePop(MyQueue* obj)
{
int ret=0;
if(StackEmpty(&obj->s2))
{
if(StackEmpty(&obj->s1))
{
return 0;
}
while(!StackEmpty(&obj->s1))
{
StackPush(&obj->s2,StackTop(&obj->s1));
StackPop(&obj->s1);
}
}
ret=StackTop(&obj->s2);
StackPop(&obj->s2);
return ret;
}
int myQueuePeek(MyQueue* obj)
{
int ret=0;
if(StackEmpty(&obj->s2))
{
if(StackEmpty(&obj->s1))
{
return 0;
}
while(!StackEmpty(&obj->s1))
{
StackPush(&obj->s2,StackTop(&obj->s1));
StackPop(&obj->s1);
}
}
ret=StackTop(&obj->s2);
return ret;
}
bool myQueueEmpty(MyQueue* obj)
{
if(StackEmpty(&obj->s1)&&StackEmpty(&obj->s2))
{
return true;
}
return false;
}
void myQueueFree(MyQueue* obj)
{
StackDestroy(&obj->s1);
StackDestroy(&obj->s2);
free(obj);
}