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

225. Implement Stack using Queues

2016-07-04 16:21 246 查看
class Stack {
public:
// Push element x onto stack.
void push(int x) {
Q.push(x);
}

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

// Get the top element.
int top() {
int len = Q.size();
for (int i = 1; i < len; i++){
Q.push(Q.front());
Q.pop();
}
int front = Q.front();
Q.push(front);
Q.pop();
return front;
}

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