题目描述
用两个栈来实现一个队列,分别完成在队列尾部插入整数(push)和在队列头部删除整数(pop)的功能。 队列中的元素为int类型。保证操作合法,即保证pop操作时队列内已有元素。
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.empty())//记得加判断
{
while(!stack1.empty())
{
stack2.push(stack1.top());//top+pop,不能直接用pop
stack1.pop();
}
}
int temp=stack2.top();
stack2.pop();
return temp;//有返回值
}
private:
stack<int> stack1;
stack<int> stack2;
};