有关集合队列和栈的转换

来源:互联网 发布:拜占庭容错算法机制 编辑:程序博客网 时间:2024/04/27 07:43

这几天刚好学习了集合Collection等子接口,下面是有关的一个面试题解决方案:

[cpp] view plaincopy
  1. #include <iostream>    
  2. #include <assert.h>    
  3. #include <queue>    
  4. using namespace std;    
  5. /*两个队列模拟一个堆栈*/    
  6. /*队列A、B  
  7. 入栈:将元素依次压入到非空的队列,第一个元素压倒对列A  
  8. 出栈:把队列A的前n-1个元素倒到队列B,把第n个元素去掉。此时数据在B中,下次操作,则对B操作。  
  9. 栈顶:把队列A的前n-1个元素倒到队列B,把第n个元素作为栈顶*/    
  10. template <typename T>    
  11. class MyStack    
  12. {    
  13. public:    
  14.     //入栈,第一个元素进到队列deque1,以后每个元素进到非空的队列    
  15.     void  push(const T &element) ;   
  16.       
  17.     //出栈,将非空队列的前n-1个元素转移到另一个空的队列,删除非空队列的第n个元素    
  18.     void pop() ;   
  19.       
  20.     //栈顶元素,将非空队列的前n-1个元素转移到另一个空的队列,将非空队列的第n个元素返回    
  21.     T top();    
  22.       
  23.     //栈是否为空    
  24.     bool empty()    
  25.     {    
  26.         return (q1.empty()&&q2.empty());    
  27.     }    
  28. private:    
  29.     queue<T> q1;    
  30.     queue<T> q2;    
  31. };    
  32.   
  33. template<typename T> void MyStack<T>::push(const T &element)  
  34. {    
  35.     if (q1.empty() && q2.empty())    
  36.     {    
  37.         q1.push(element);    
  38.     }    
  39.     else if (!q1.empty() && q2.empty())    
  40.     {    
  41.         q1.push(element);    
  42.     }    
  43.     else if (q1.empty() && !q2.empty())    
  44.     {    
  45.         q2.push(element);    
  46.     }    
  47. }    
  48. template<typename T>void MyStack<T>::pop()  
  49. {    
  50.     if (!q1.empty())    
  51.     {    
  52.         int size = q1.size();    
  53.         for (int i=0; i<size-1; i++)    
  54.         {    
  55.             q2.push(q1.front());    
  56.             q1.pop();    
  57.         }    
  58.         q1.pop();    
  59.     }    
  60.     else    
  61.     {    
  62.         int size = q2.size();    
  63.         for (int i=0; i<size-1; i++)    
  64.         {    
  65.             q1.push(q2.front());    
  66.             q2.pop();    
  67.         }    
  68.         q2.pop();    
  69.     }    
  70. }   
  71. template <typename T>T MyStack<T>::top()  
  72. {    
  73.     if (!q1.empty())    
  74.     {    
  75.         int size = q1.size();    
  76.         for (int i=0; i<size-1; i++)    
  77.         {    
  78.             q2.push(q1.front());    
  79.             q1.pop();    
  80.         }    
  81.         T temp = q1.front();    
  82.         q1.pop();    
  83.         q2.push(temp);    
  84.         return temp;    
  85.     }    
  86.     else    
  87.     {    
  88.         int size = q2.size();    
  89.         for (int i=0; i<size-1; i++)    
  90.         {    
  91.             q1.push(q2.front());    
  92.             q2.pop();    
  93.         }    
  94.         T temp = q2.front();    
  95.         q2.pop();    
  96.         q1.push(temp);    
  97.         return temp;    
  98.     }    
  99. }     
  100. int main(int argc, char *argv[])    
  101. {    
  102.     MyStack<int> my;    
  103.     for (int i=0; i<10; i++)    
  104.     {    
  105.         my.push(i);    
  106.     }    
  107.     while (!my.empty())    
  108.     {    
  109.         cout<<my.top()<<" ";    
  110.         my.pop();    
  111.     }    
  112.     cout<<endl;    
  113.     return 0;  
  114. }    
更多的体现在解决问题的思想上!

0 0
原创粉丝点击