1.题目
2.思路—双栈
思路与双队列实现栈类似:
将一个栈当作输入栈,用于压入push传入的数据;另一个栈当作输出栈,用于pop和peek操作。
每次 pop或 peek时,若输出栈outStack为空则将输入栈inStack的全部数据依次出栈并入输出栈,这样输出栈从栈顶往栈底的顺序就是队列从队首往队尾的顺序。
图解:
3.参考代码
typedef struct {int* stk;int stkSize;int stkCapacity;
} Stack;Stack* stackCreate(int cpacity) {Stack* ret = malloc(sizeof(Stack));ret->stk = malloc(sizeof(int) * cpacity);ret->stkSize = 0;ret->stkCapacity = cpacity;return ret;
}void stackPush(Stack* obj, int x) {obj->stk[obj->stkSize++] = x;
}void stackPop(Stack* obj) {obj->stkSize--;
}int stackTop(Stack* obj) {return obj->stk[obj->stkSize - 1];
}bool stackEmpty(Stack* obj) {return obj->stkSize == 0;
}void stackFree(Stack* obj) {free(obj->stk);
}typedef struct {Stack* inStack;Stack* outStack;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* ret = malloc(sizeof(MyQueue));ret->inStack = stackCreate(100);ret->outStack = stackCreate(100);return ret;
}void in2out(MyQueue* obj) {while (!stackEmpty(obj->inStack)) {stackPush(obj->outStack, stackTop(obj->inStack));stackPop(obj->inStack);}
}void myQueuePush(MyQueue* obj, int x) {stackPush(obj->inStack, x);
}int myQueuePop(MyQueue* obj) {if (stackEmpty(obj->outStack)) {in2out(obj);}int x = stackTop(obj->outStack);stackPop(obj->outStack);return x;
}int myQueuePeek(MyQueue* obj) {if (stackEmpty(obj->outStack)) {in2out(obj);}return stackTop(obj->outStack);
}bool myQueueEmpty(MyQueue* obj) {return stackEmpty(obj->inStack) && stackEmpty(obj->outStack);
}void myQueueFree(MyQueue* obj) {stackFree(obj->inStack);stackFree(obj->outStack);
}