zoukankan      html  css  js  c++  java
  • 第三十八课 两个有趣的问题

    问题:

    进栈到出栈的数据元素的转移在出栈(stack_out)为空的时候发生。

    用栈实现队列:

     1 #include <iostream>
     2 #include "LinkStack.h"
     3 #include "LinkQueue.h"
     4 
     5 using namespace std;
     6 using namespace DTLib;
     7 
     8 template < typename T >
     9 class StackToQueue : public Queue<T>
    10 {
    11 protected:
    12     mutable LinkStack<T> m_stack_in;
    13     mutable LinkStack<T> m_stack_out;
    14 
    15     void move() const
    16     {
    17         if( m_stack_out.size() == 0 )
    18         {
    19             while( m_stack_in.size() > 0 )
    20             {
    21                 m_stack_out.push(m_stack_in.top());
    22                 m_stack_in.pop();
    23             }
    24         }
    25     }
    26 
    27 public:
    28     void add(const T& e)   // O(1)
    29     {
    30         m_stack_in.push(e);
    31     }
    32 
    33     void remove()   // O(n)
    34     {
    35         move();
    36 
    37         if( m_stack_out.size() > 0 )
    38         {
    39             m_stack_out.pop();
    40         }
    41         else
    42         {
    43             THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");
    44         }
    45     }
    46 
    47     T front() const  // O(n)
    48     {
    49         move();
    50 
    51         if( m_stack_out.size() > 0 )
    52         {
    53             return m_stack_out.top();
    54         }
    55         else
    56         {
    57             THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");
    58         }
    59     }
    60 
    61     void clear()  // O(n)
    62     {
    63         m_stack_in.clear();
    64         m_stack_out.clear();
    65     }
    66 
    67     int length() const   // O(1)
    68     {
    69         return m_stack_in.size() + m_stack_out.size();
    70     }
    71 };
    72 
    73 int main()
    74 {
    75     StackToQueue<int> sq;
    76 
    77     for(int i = 0; i < 10; i++)
    78     {
    79         sq.add(i);
    80     }
    81 
    82     while( sq.length() > 0 )
    83     {
    84         cout << sq.front() << endl;
    85         sq.remove();
    86     }
    87 
    88     return 0;
    89 }

    结果如下:

    用队列实现栈:

     

    进栈时直接进,出栈时,先将前n-1个元素转移到另一个队列,然后剩下的那一个元素出去即可。

    两个队列来回倒换。

    程序如下:

      1 #include <iostream>
      2 #include "LinkStack.h"
      3 #include "LinkQueue.h"
      4 
      5 using namespace std;
      6 using namespace DTLib;
      7 
      8 template < typename T >
      9 class StackToQueue : public Queue<T>
     10 {
     11 protected:
     12     mutable LinkStack<T> m_stack_in;
     13     mutable LinkStack<T> m_stack_out;
     14 
     15     void move() const
     16     {
     17         if( m_stack_out.size() == 0 )
     18         {
     19             while( m_stack_in.size() > 0 )
     20             {
     21                 m_stack_out.push(m_stack_in.top());
     22                 m_stack_in.pop();
     23             }
     24         }
     25     }
     26 
     27 public:
     28     void add(const T& e)   // O(1)
     29     {
     30         m_stack_in.push(e);
     31     }
     32 
     33     void remove()   // O(n)
     34     {
     35         move();
     36 
     37         if( m_stack_out.size() > 0 )
     38         {
     39             m_stack_out.pop();
     40         }
     41         else
     42         {
     43             THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");
     44         }
     45     }
     46 
     47     T front() const  // O(n)
     48     {
     49         move();
     50 
     51         if( m_stack_out.size() > 0 )
     52         {
     53             return m_stack_out.top();
     54         }
     55         else
     56         {
     57             THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");
     58         }
     59     }
     60 
     61     void clear()  // O(n)
     62     {
     63         m_stack_in.clear();
     64         m_stack_out.clear();
     65     }
     66 
     67     int length() const   // O(1)
     68     {
     69         return m_stack_in.size() + m_stack_out.size();
     70     }
     71 };
     72 
     73 template < typename T >
     74 class QueueToStack : public Stack<T>
     75 {
     76 protected:
     77     LinkQueue<T> m_queue_1;
     78     LinkQueue<T> m_queue_2;
     79 
     80     LinkQueue<T>* m_pIn;
     81     LinkQueue<T>* m_pOut;
     82 
     83     void move() const  // O(n)
     84     {
     85         int n = m_pIn->length() - 1;
     86 
     87         for(int i = 0; i < n; i++)
     88         {
     89             m_pOut->add(m_pIn->front());
     90 
     91             m_pIn->remove();
     92         }
     93     }
     94 
     95     void swap()  // O(1)
     96     {
     97         LinkQueue<T>* temp = NULL;
     98 
     99         temp = m_pIn;
    100         m_pIn = m_pOut;
    101         m_pOut = temp;
    102     }
    103 
    104 public:
    105     QueueToStack()   // O(1)
    106     {
    107         m_pIn = &m_queue_1;
    108         m_pOut = &m_queue_2;
    109     }
    110 
    111     void push(const T& e)   // O(1)
    112     {
    113         m_pIn->add(e);
    114     }
    115 
    116     void pop()  // O(n)
    117     {
    118         if( m_pIn->length() > 0 )
    119         {
    120             move();
    121 
    122             m_pIn->remove(); //出栈
    123 
    124             swap();
    125         }
    126         else
    127         {
    128             THROW_EXCEPTION(InvalidOperationException, "No element in current stack...");
    129         }
    130     }
    131 
    132     T top() const  //  O(n)
    133     {
    134         if( m_pIn->length() > 0 )
    135         {
    136             move();
    137 
    138             return m_pIn->front();
    139         }
    140         else
    141         {
    142             THROW_EXCEPTION(InvalidOperationException, "No element in current stack...");
    143         }
    144     }
    145 
    146     void clear()  // O(n)
    147     {
    148         m_queue_1.clear();
    149         m_queue_2.clear();
    150     }
    151     int size() const   // O(1)
    152     {
    153         return m_queue_1.length() + m_queue_2.length();
    154     }
    155 };
    156 
    157 int main()
    158 {
    159     QueueToStack<int> qs;
    160 
    161     for(int i = 0; i < 10; i++)
    162     {
    163         qs.push(i);
    164     }
    165 
    166     while( qs.size() > 0 )
    167     {
    168         cout << qs.top() << endl;
    169         qs.pop();
    170     }
    171 
    172     return 0;
    173 }

    结果如下:

     小结:

  • 相关阅读:
    Got05
    Git07
    Git09
    Git11
    Git10
    Git13
    Git12
    Git14
    Listview点击跳转页面
    《三个和尚》观后感
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9657770.html
Copyright © 2011-2022 走看看