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

225. Implement Stack using Queues

2016-07-08 21:18 447 查看
Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.

empty() -- Return whether the stack is empty.
用一个队列实现栈。
class Stack {
queue<int> Q;
public:
// Push element x onto stack.
void push(int x) {
Q.push(x);
}

// Removes the element on top of the stack.
void pop() {
for (int i=0; i < Q.size()-1; i++)
{
Q.push(Q.front());
Q.pop();
}
Q.pop();
}

// Get the top element.
int top() {
return Q.back();
}

// Return whether the stack is empty.
bool empty() {
return Q.empty();
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: