//参考了很多网上的代码。。。不是原创
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int result=0;
int temp=0;
if(stack2.empty())
{
while(!stack1.empty())
{
temp=stack1.top();
stack2.push(temp);
stack1.pop();
}
}
result=stack2.top();
stack2.pop();
return result;
}
private:
stack<int> stack1;
stack<int> stack2;
};
本文介绍了一种使用两个栈来实现队列的方法。通过这种方式,可以在不需要额外数据结构的情况下实现队列的基本操作,如push和pop。代码示例中详细展示了如何在C++中实现这一逻辑。
1万+

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



