题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution
{
public:
void push(int node) {
mystack1.push(node);
}
int pop() {
if (mystack2.empty()){
if (mystack1.empty()){
return -1;
}
while (!mystack1.empty())
{
mystack2.push(mystack1.top());
mystack1.pop();
}
}
int ret = mystack2.top();
mystack2.pop();
return ret;
}
private:
stack<int> mystack1;
stack<int> mystack2;
};