两个栈来实现一个队列的C++代码

转载:http://blog.youkuaiyun.com/stpeace/article/details/46765343


利用两个栈来实现一个队列, 这个问题很常见。  最关键的是要有好的思路, 至于实现, 那是很简单的事情了。 在本文中, 也想说说自己的思路, 但是, 我觉得用代码来表述思路更符合我的习惯, 也是我的菜, 所以, 仅仅给出代码。 如有需要, 大家可以根据代码来理解思路。

       OK, 没有必要废话了, 直接上代码:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <stack>  
  3. using namespace std;  
  4.   
  5.   
  6. // 用两个stack实现一个queue, 以int类型为例吧  
  7. class MyQueque  
  8. {  
  9. private:  
  10.     stack<int> s1; // s1负责入队  
  11.     stack<int> s2; // s2负责出队  
  12.       
  13. public:  
  14.     // 入队  
  15.     void push(int x)  
  16.     {  
  17.         s1.push(x);  
  18.     }  
  19.   
  20.     // 出队  
  21.     void pop()  
  22.     {  
  23.         if(!s2.empty())  
  24.         {  
  25.             s2.pop();  
  26.         }  
  27.         else  
  28.         {  
  29.             while(!s1.empty())  
  30.             {  
  31.                 int tmp = s1.top();  
  32.                 s1.pop();  
  33.                 s2.push(tmp);  
  34.             }  
  35.   
  36.             s2.pop();  
  37.         }  
  38.     }  
  39.   
  40.     // 取头  
  41.     int front()  
  42.     {  
  43.         if(!s2.empty())  
  44.         {  
  45.             return s2.top();  
  46.         }  
  47.   
  48.         while(!s1.empty())  
  49.         {  
  50.             int tmp = s1.top();  
  51.             s1.pop();  
  52.             s2.push(tmp);  
  53.         }  
  54.   
  55.         return s2.top();  
  56.     }  
  57.   
  58.     // 取尾  
  59.     int back()  
  60.     {  
  61.         if(!s1.empty())  
  62.         {  
  63.             return s1.top();  
  64.         }  
  65.   
  66.         while(!s2.empty())  
  67.         {  
  68.             int tmp = s2.top();  
  69.             s2.pop();  
  70.             s1.push(tmp);  
  71.         }  
  72.   
  73.         return s1.top();  
  74.     }  
  75.   
  76.     // 求大小  
  77.     int size()  
  78.     {  
  79.         return s1.size() + s2.size();  
  80.     }  
  81.   
  82.     // 判断是否为空  
  83.     bool empty()  
  84.     {  
  85.         if(s1.empty() && s2.empty())  
  86.         {  
  87.             return true;  
  88.         }  
  89.   
  90.         return false;  
  91.     }  
  92. };  
  93.   
  94.   
  95. int main()  
  96. {  
  97.     {  
  98.         MyQueque que;  
  99.         que.push(1);  
  100.         que.push(2);  
  101.         que.push(3);  
  102.         que.push(4);  
  103.         que.push(5);  
  104.   
  105.         while(!que.empty())  
  106.         {  
  107.             cout << que.front() << endl;  
  108.             que.pop();  
  109.         }  
  110.   
  111.         cout << "-------------------" << endl;  
  112.     }  
  113.   
  114.   
  115.     {  
  116.         MyQueque que;  
  117.         que.push(1);  
  118.         que.push(2);  
  119.         que.push(3);  
  120.         que.push(4);  
  121.         que.push(5);  
  122.   
  123.         while(!que.empty())  
  124.         {  
  125.             cout << que.size() << endl;  
  126.             que.pop();  
  127.         }  
  128.   
  129.         cout << "-------------------" << endl;  
  130.     }  
  131.   
  132.   
  133.     {  
  134.         MyQueque que;  
  135.         que.push(1);  
  136.         que.push(2);  
  137.         que.push(3);  
  138.         que.pop();  
  139.         que.pop();  
  140.         que.push(4);  
  141.         que.push(5);  
  142.   
  143.         while(!que.empty())  
  144.         {  
  145.             cout << que.front() << endl;  
  146.             que.pop();  
  147.         }  
  148.   
  149.         cout << "-------------------" << endl;  
  150.     }  
  151.   
  152.     return 0;  
  153. }  
       结果:

1
2
3
4
5
-------------------
5
4
3
2
1
-------------------
3
4
5
-------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值