// 使用栈实现队列 #include "stack" #include "iostream" using namespace std; class MyQueue { private: stack output; stack input; public: MyQueue() { } void push(int x) { input.push(x); } int pop() { int res = peek(); output.pop(); return res; } int peek() { if (output.empty()) { while (!input.empty()) { output.push(input.top()); input.pop(); } } int res = output.top(); return res; } bool empty() { return input.empty() && output.empty(); } }; /** * Your MyQueue object will be instantiated and called as such: * MyQueue* obj = new MyQueue(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->peek(); * bool param_4 = obj->empty(); */