用两个栈实现队列

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
 1 class Solution
 2 {
 3 public:
 4     void push(int node) {
 5         stack1.push(node);
 6     }
 7 
 8     bool pop() {
 9         if(stack2.empty()&&stack1.empty())
10             return false;
11         if(!stack2.empty())
12         {
13             stack2.pop();
14             return true;
15         }
16         if(stack2.empty()&&!stack1.empty())
17         {
18             while(!stack1.empty())
19             {
20                 stack2.push(stack1.top());
21                 stack1.pop();
22             }
23             stack2.pop();
24             return true;
25         }
26     }
27 
28 private:
29     stack<int> stack1;
30     stack<int> stack2;
31 };
32 
33 /*
34     c++ stl栈stack的成员函数介绍
35 
36     操作    比较和分配堆栈
37 
38     empty()    堆栈为空则返回真
39 
40     pop()    移除栈顶元素
41 
42     push()    在栈顶增加元素
43 
44     size()    返回栈中元素数目
45 
46     top()    返回栈顶元素
47 
48 */
原文地址:https://www.cnblogs.com/firstcxj/p/4822443.html