题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
while(!stack1.empty())
{
int top = stack1.top();
stack1.pop();
stack2.push(top);
}
int ans = stack2.top();
stack2.pop();
while(!stack2.empty())
{
int top = stack2.top();
stack2.pop();
stack1.push(top);
}
return ans;
}
private:
stack<int> stack1;
stack<int> stack2;
};
本文详细介绍了如何利用两个栈来实现队列的基本操作,包括如何进行元素的入队(push)和出队(pop),提供了具体的类实现和操作流程。
1301

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



