Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 225. Implement Stack using Queues
題目:

解答:
class MyStack {
private:
queue<int> q;
public:
MyStack() {
}
void push(int x) {
q.push(x);
int size = q.size();
for (int i = 0; i < size - 1; ++i) {
q.push(q.front());
q.pop();
}
}
int pop() {
int r = q.front(); q.pop();
return r;
}
int top() {
return q.front();
}
bool empty() {
return q.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();
*/