剑指offer两个栈实现队列
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int node;
if(stack2.empty())
{
while(!stack1.empty())
{
stack2.push(stack1.top());//top表示返回栈顶元素,但不删除栈顶元素
stack1.pop();//表示弹出栈顶元素,但不返回其值
}
}
node = stack2.top();
stack2.pop();
return node;
}
private:
stack<int> stack1;
stack<int> stack2;
};