#include <iostream>
#include <stack>
using namespace std;classMyQueue{private:
stack<int> s1, s2;//主栈s1,辅助栈s2public:/** Initialize your data structure here. */MyQueue(){}/** Push element x to the back of queue. */voidpush(int x){ s1.push(x);}//队列的入队 就是入到主栈s1/** Removes the element from in front of queue and returns that element. */intpop(){//队列出队 将主栈s1的元素全部出栈 然后再按顺序入栈s2 //s2出栈就是队列出队 (相当于把顺序颠倒)while(s2.empty()){while(!s1.empty()){
s2.push(s1.top());
s1.pop();}}int ret = s2.top();
s2.pop();return ret;}/** Get the front element. */intpeek(){//获取队首元素 将主栈s1的元素全部出栈 然后再按顺序入栈s2 //s2栈顶元素就是队首元素 (相当于把顺序颠倒)while(s2.empty()){while(!s1.empty()){
s2.push(s1.top());
s1.pop();}}return s2.top();}/** Returns whether the queue is empty. */
bool empty(){return s1.empty()&& s2.empty();}//判断队列是否为空//s1s2都为空则队列为空};intmain(){MyQueue* obj =newMyQueue();
obj->push(1);
obj->push(2);int param_2 = obj->pop();int param_3 = obj->peek();
bool param_4 = obj->empty();
cout << param_2 << endl;//输出1
cout << param_3 << endl;//输出2
cout << param_4 << endl;//输出0return0;}