剑指offer-用两个栈实现队列
入队列简单,直接在A栈push
出队列时
1、B栈不为空,出栈;
2、B栈空,将A栈的数全部放入B栈,在出栈
1 class Solution 2 { 3 public: 4 void push(int node) { 5 stack1.push(node); 6 } 7 8 int pop() { 9 if(stack2.empty()){ 10 while(!stack1.empty()){ 11 int node =stack1.top(); 12 stack1.pop(); 13 stack2.push(node); 14 } 15 } 16 int node =stack2.top(); 17 stack2.pop(); 18 return node; 19 } 20 21 private: 22 stack<int> stack1;//A 23 stack<int> stack2;//B 24 };