LeetCode225. 用队列实现栈
https://leetcode-cn.com/problems/implement-stack-using-queues/
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
queue<int>qu;
/** Push element x onto stack. */
void push(int x) {
qu.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int n=qu.size()-1;
while (n--){
qu.push(qu.front());
qu.pop();
}
int t=qu.front();
qu.pop();
return t;
}
/** Get the top element. */
int top() {
return qu.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return qu.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/