232.用栈实现队列
注意:
1,构造函数不需要
2,需要有两个成员变量 in out
代码:
1 class MyQueue { 2 public: 3 stack<int> in; 4 stack<int>out; 5 MyQueue() { 6 7 } 8 9 void push(int x) { 10 in.push(x); 11 } 12 13 int pop() { 14 if (out.empty()) 15 { 16 while (in.empty() != true) 17 { 18 out.push(in.top()); 19 in.pop(); 20 } 21 } 22 23 int result = out.top(); 24 out.pop(); 25 return result; 26 } 27 28 int peek() { 29 int result = pop(); 30 out.push(result); 31 32 return result; 33 } 34 35 bool empty() { 36 if (out.empty() && in.empty()) 37 return true; 38 39 return false; 40 } 41 }; 42 43 /** 44 * Your MyQueue object will be instantiated and called as such: 45 * MyQueue* obj = new MyQueue(); 46 * obj->push(x); 47 * int param_2 = obj->pop(); 48 * int param_3 = obj->peek(); 49 * bool param_4 = obj->empty(); 50 */
标签:第九天,obj,队列,随想录,int,result,MyQueue,empty,out From: https://www.cnblogs.com/smartisn/p/17485786.html