您的位置:首页 > 产品设计 > UI/UE

225 Implement Stack using Queues

2015-11-14 20:38 393 查看
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;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: