剑指offer04:用两个栈实现队列

 1 class Solution
 2 {
 3 public:
 4     void push(int node) {
 5         stack1.push(node);
 6     }
 7 
 8     int pop() {
 9         int pdata = 0;
10         if(!stack2.empty()){
11             pdata = stack2.top();
12             stack2.pop();
13         }
14         else{
15             if(!stack1.empty()){
16                 while(!stack1.empty()){
17                     stack2.push(stack1.top());
18                     stack1.pop();
19                 }
20                 pdata = stack2.top();
21                 stack2.pop();
22             }
23             else{
24                 return -1;
25             }
26         }
27         return pdata;
28     }
29 
30 private:
31     stack<int> stack1;
32     stack<int> stack2;
33 };

 

posted on 2018-03-26 21:22  CreatorKou  阅读(104)  评论(0编辑  收藏  举报

导航