1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class MyQueue { public: MyQueue() : s_in() , s_out() {} void push(int x) { s_in.push(x); } int pop() { if(s_out.empty()) while(!s_in.empty()) { s_out.push(s_in.top()); s_in.pop(); } int tmp=s_out.top(); s_out.pop(); return tmp; } int peek() { int tmp=this->pop(); s_out.push(tmp); return tmp; } bool empty() { return s_in.empty() && s_out.empty(); } private: stack<int> s_in; stack<int> s_out; };
|