class Stack {
public:
// Push element x onto stack.
void push(int x) {
q1.push(x);
}
// Removes the element on top of the stack.
void pop() {
while(q1.size()!=1)
{
q2.push(q1.front());
q1.pop();
}
q1.pop();
q1 = q2;
while(!q2.empty())
q2.pop();
}
// Get the top element.
int top() {
return q1.back();
}
// Return whether the stack is empty.
bool empty() {
return q1.empty();
}
private:
queue<int> q1;
queue<int> q2;
};
225 Implement Stack using Queues
最新推荐文章于 2022-10-14 12:18:40 发布