#define MAXSIZE 10000
typedef struct{
int *data;
int top;
}Stack;
bool emptyStack(Stack *S){
if(S->top==0){
return 1;
}
else{
return 0;
}
}
void pushStack(Stack *S,int val){
if(S->top==MAXSIZE){
return;
}
else{
S->data[S->top++]=val;
}
}
int popStack(Stack *S){
if(!emptyStack(S)){
return S->data[--S->top];
}
else{
return -1;
}
}
typedef struct {
Stack *a;
Stack *b;
} CQueue;
CQueue* cQueueCreate() {
CQueue *obj=(CQueue *)malloc(sizeof(CQueue));
obj->a=(Stack *)malloc(sizeof(Stack));
obj->a->data=(int *)malloc(MAXSIZE*sizeof(int));
obj->a->top=0;
obj->b=(Stack *)malloc(sizeof(Stack));
obj->b->data=(int *)malloc(MAXSIZE*sizeof(int));
obj->b->top=0;
return obj;
}
void cQueueAppendTail(CQueue* obj, int value) {
pushStack(obj->a,value);
}
int cQueueDeleteHead(CQueue* obj) {
if(!emptyStack(obj->b)){
return popStack(obj->b);
}
else{
if(emptyStack(obj->a)){
return -1;
}
else{
int temp;
while(!emptyStack(obj->a)){
temp=popStack(obj->a);
pushStack(obj->b,temp);
}
return popStack(obj->b);
}
}
}
void cQueueFree(CQueue* obj) {
free(obj->a->data);
free(obj->b->data);
free(obj->a);
free(obj->b);
free(obj);
}
/**
* Your CQueue struct will be instantiated and called as such:
* CQueue* obj = cQueueCreate();
* cQueueAppendTail(obj, value);
* int param_2 = cQueueDeleteHead(obj);
* cQueueFree(obj);
*/
力扣第一次做题
最新推荐文章于 2024-06-27 16:39:17 发布