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