就是个模拟;
如果把队头当做栈顶,只是push函数比较麻烦;
如果把队尾当做栈顶,pop和top函数都比较麻烦;
这里采用第一种方法;
class Stack {
public:
void push(int x) {
Q.push(x);
int size = Q.size() - 1;
for (int i = 0; i < size; i++) {
Q.push(Q.front());
Q.pop();
}
}
void pop() {
Q.pop();
}
int top() {
return Q.front();
}
bool empty() {
return Q.empty();
}
private:
queue<int> Q;
};
LeetCode Implement Stack using Queues
原文地址:http://blog.csdn.net/u012925008/article/details/46480649