zoukankan      html  css  js  c++  java
  • 两个队列实现栈

    前面已经实现了两个栈实现队列的功能(http://www.cnblogs.com/ivorfeng/archive/2013/05/01/3053206.html),今天就实现利用两个队列实现栈的push和pop功能。


    思路:已有queue1和queue2;

    进栈:初始时,两个队列均为空,元素压入queue1;之后需要判断压入非空的队列中;

    出栈:出栈时,两个队列必有一个为空,另一个非空,因此需要把非空的队列只留下最后一个元素,其他均转移到另一个队列中;再把之前剩下的一个元素输出。

    ps:出栈的时候,一开始想着统一代码,想把已经转移到另一个队列的元素再转回来,这样就可以保持每次只对其中一个队列(queue1)操作pop;后来经小涛同志的提醒,发现两个队列会有一个为空,可以利用这个特点判断对那个队列操作,避免了转移带来的额外复杂度。


    实现代码如下

    code:

     1 template<typename T>class CStack
     2 {
     3 public:
     4     CStack(void);
     5     ~CStack(void);
     6     void push(T element);
     7     T pop();
     8 private:
     9     queue<T> queue1;
    10     queue<T> queue2;
    11 };
    12 
    13 template<typename T> CStack<T>::CStack(void)
    14 {
    15 };
    16 template<typename T> CStack<T>::~CStack(void)
    17 {
    18 };
    19 //压入栈
    20 template<typename T>void CStack<T>::push(T element)
    21 {
    22     if (queue2.empty())
    23     {
    24         queue1.push(element);
    25     }
    26     else
    27     {
    28         queue2.push(element);
    29     }
    30 
    31 };
    32 //出栈
    33 template<typename T>T CStack<T>::pop()
    34 {
    35     //queue2空,则操作queue1
    36     if (queue2.empty())
    37     {
    38         if (queue1.empty())
    39         {
    40             throw new exception("Stack is empty");
    41         }
    42         //queue1留下一个元素,其他都转移到queue2中
    43         while (queue1.size() > 1)
    44         {
    45             T& tmp1 = queue1.front(); 
    46             queue2.push(tmp1);
    47             queue1.pop();
    48         }
    49         T tmp2 = queue1.front();
    50         queue1.pop();
    51         return tmp2;
    52     }
    53     else//否则操作queue2
    54     {
    55         if (queue2.empty())
    56         {
    57             throw new exception("Stack is empty");
    58         }
    59         while (queue2.size() > 1)
    60         {
    61             T& tmp1 = queue2.front(); 
    62             queue1.push(tmp1);
    63             queue2.pop();
    64         }
    65         T tmp2 = queue2.front();
    66         queue2.pop();
    67         return tmp2;
    68     }
    69 };

    测试代码:

     1 //---------------------------TEST--------------------//
     2 void Test(char actual, char expected)
     3 {
     4     if(actual == expected)
     5         printf("Test passed.\n");
     6     else
     7         printf("Test failed.\n");
     8 }
     9 
    10 int main()
    11 {
    12     CStack<char> mystack;
    13 
    14     mystack.push('a');
    15     mystack.push('b');
    16     mystack.push('c');
    17 
    18     char head = mystack.pop();
    19     Test(head, 'c');
    20 
    21     head = mystack.pop();
    22     Test(head, 'b');
    23 
    24     mystack.push('d');
    25     head = mystack.pop();
    26     Test(head, 'd');
    27 
    28     mystack.push('e');
    29     head = mystack.pop();
    30     Test(head, 'e');
    31 
    32     head = mystack.pop();
    33     Test(head, 'a');
    34 
    35     return 0;
    36 }
  • 相关阅读:
    华为实习日记——第二十三天
    华为实习日记——第二十二天
    华为实习日记——第二十一天
    华为实习日记——第二十天
    HDU 5102 The K-th Distance(模拟)
    HDU 4113 Construct the Great Wall(插头dp)
    UVALive 4849 String Phone(2-sat、01染色)
    HDU 4859 海岸线(最大流最小割)
    HDU 3879 Base Station(最大权闭合子图)
    POJ 3155 Hard Life(最大密度子图)
  • 原文地址:https://www.cnblogs.com/ivorfeng/p/3057808.html
Copyright © 2011-2022 走看看