用两个栈实现队列

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
 
 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         {
11             while(!stack1.empty())
12             {
13                 stack2.push(stack1.top());
14                 stack1.pop();
15             }
16         }
17         if(!stack2.empty())
18         {
19             int tem = stack2.top();
20             stack2.pop();
21             return tem;
22         }
23         else
24         {
25             return -1;
26         }
27     }
28 
29 private:
30     stack<int> stack1;
31     stack<int> stack2;
32 };

 

posted @ 2016-01-21 15:44  小爷  阅读(107)  评论(0)    收藏  举报