使用两个栈来模拟队列,队列的模式是先进先出,而栈的模式是先进后出,为了用栈实现先进先出,在出队的时候,需要另一个栈来做辅助,把入的那个栈的每一个元素,都弹出并放到另一个栈里面,这样最先入队的节点就在另一个栈的栈顶了。
下面是C++语言实现的代码:
class MyQueue {
private:
stack<int> s1;
stack<int> s2;
void moveS2ToS1() {
while (s2.size() > 0) {
int t = s2.top();
s2.pop();
s1.push(t);
}
}
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
s2.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if (s1.size() == 0) {
moveS2ToS1();
}
int ret = s1.top();
s1.pop();
return ret;
}
/** Get the front element. */
int peek() {
if (s1.size() == 0) {
moveS2ToS1();
}
return s1.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.empty() && s2.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/