class Solution
{
public:
void push(int node)
{
stack1.push(node);
}
int pop()
{
if(stack2.size()<=0)
{
while(!stack1.empty())
{
int data = stack1.top();
stack2.push(data);
stack1.pop();
}
}
int head = stack2.top();
stack2.pop();
return head;
}
private:
stack stack1;
stack stack2;
};
双栈实现队列

本文介绍了一种使用两个栈来实现队列的方法。通过这种方式,可以在不需要额外数据结构的情况下实现队列的基本操作,如push和pop。当进行pop操作时,如果辅助栈为空,则将主栈的所有元素依次弹出并压入辅助栈中,最后返回辅助栈的栈顶元素。
910

被折叠的 条评论
为什么被折叠?



