标签:leetcode
Implement Stack using Queues
Implement the following operations of a stack using queues.
push
to back, peek/pop from front, size,
and is empty operations are valid.
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
class Stack {
public:
queue<int>q1,q2;
// Push element x onto stack.
void push(int x) {
q1.push(x);
}
// Removes the element on top of the stack.
void pop() {
while (!q1.empty()) {
if (q1.size() > 1)
q2.push(q1.front());
q1.pop();
}
while (!q2.empty()) {
q1.push(q2.front());
q2.pop();
}
}
// Get the top element.
int top() {
return q1.back();
}
// Return whether the stack is empty.
bool empty() {
return q1.empty();
}
};版权声明:本文为博主原创文章,未经博主允许不得转载。
leetcode-225-Implement Stack using Queues
标签:leetcode
原文地址:http://blog.csdn.net/u014705854/article/details/47259619