zoukankan      html  css  js  c++  java
  • 用两个栈创建队列

    1.第一种方法:

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include<iostream>
      3 #include<stack>
      4 using namespace std;
      5 
      6 //Pop , push, front, back, empty;
      7 template<class T>
      8 class MyQueue
      9 {
     10 public:
     11     //判断队列是否为空。    empty
     12     bool empty()
     13     {
     14         if (head.empty() && reak.empty())
     15             return true;
     16         return false;
     17     }
     18 
     19     //队列添加元素            push
     20     void push(T val)
     21     {
     22         head.push(val);
     23     }
     24 
     25     //删除队列的元素: 队列的性质:先进先出        pop
     26     void Pop()
     27     {
     28         if (this->empty())                    //判断队列知否为空
     29             throw exception("MyQueue empty");
     30         if (!head.empty())                    //队列不为空,
     31         {
     32             if (reak.empty())                //中转栈为空
     33             {
     34                 while (!head.empty())        //将数据拷进中转栈中,这样,就可以使第一个元素暴漏出来
     35                 {
     36                     reak.push(head.top());
     37                     head.pop();
     38                 }
     39             }
     40         }
     41         reak.pop();                        //删除中转栈的栈顶元素,即队列的第一个元素
     42     }
     43 
     44     
     45     //返回队列的队头元素
     46     T& Front()
     47     {
     48         if (this->empty())                    //判断队列知否为空
     49             throw exception("MyQueue empty");
     50         if (!head.empty())
     51         {
     52             if (reak.empty())
     53             {
     54                 while (!head.empty())
     55                 {
     56                     reak.push(head.top());
     57                     head.pop();
     58                 }
     59             }
     60         }
     61         return reak.top();
     62     }
     63 
     64 
     65     //返回队列的队尾元素
     66     T& Back()
     67     {
     68         if (this->empty())                    //判断队列知否为空
     69             throw exception("MyQueue empty");
     70         if (!reak.empty())
     71         {
     72             if (head.empty())
     73             {
     74                 while (!reak.empty())
     75                 {
     76                     head.push(reak.top());
     77                     reak.pop();
     78                 }
     79             }
     80         }
     81 
     82         return head.top();
     83     }
     84 
     85 private:
     86     stack<T> head;        //队列的尾
     87     stack<T> reak;        //队列的头
     88 };
     89 
     90 //测试函数
     91 void test01()
     92 {
     93     MyQueue<int> que;
     94     //给队列添加元素
     95     for (int i = 0; i < 10; i++)
     96     {
     97         que.push(i + 10);
     98     }
     99     //返回队头元素
    100     cout<<que.Front()<<endl;
    101     //返回队尾元素
    102     cout << que.Back() << endl;
    103 
    104     //删除队头元素
    105     que.Pop();
    106     que.Pop();
    107 
    108     //返回队头元素
    109     cout << que.Front() << endl;
    110     //返回队尾元素
    111     cout << que.Back() << endl;
    112     //判断队列是否为空
    113     if (que.empty())
    114     {
    115         cout << "队列为空" << endl;
    116     }
    117     else
    118     {
    119         cout << "队列不为空" << endl;
    120     }
    121 
    122 }
    123 
    124 
    125 int main()
    126 {
    127     try
    128     {
    129         test01();
    130     }
    131     catch (...){
    132         cout << "抛出其他异常" << endl;
    133     }
    134 
    135     system("pause");
    136     return EXIT_SUCCESS;
    137 }

    2.第二种方法:

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include<iostream>
      3 #include <stack>
      4 using namespace std;
      5 
      6 class Myqueue
      7 {
      8 public:
      9     //判断队列是否为空。    empty
     10     bool empty()
     11     {
     12         if (s1.empty() && s2.empty())
     13             return true;
     14         return false;
     15     }
     16 
     17     //队列添加元素            push
     18     void push(int val)
     19     {
     20         s1.push(val);
     21     }
     22 
     23     //删除队列的元素: 队列的性质:先进先出        pop
     24     void Pop()
     25     {
     26         if (this->empty())
     27         {
     28             cout << "队列为空Error!!!" << endl;
     29             return;
     30         }
     31         //先把元素拷进中转栈
     32         if (!s1.empty())
     33         {
     34             if (s2.empty())
     35             {
     36                 while (!s1.empty())
     37                 {
     38                     s2.push(s1.top());
     39                     s1.pop();
     40                 }
     41             }
     42         }
     43         //删除完元素后再拷进原来的栈
     44         if (!s2.empty())
     45         {
     46             s2.pop();
     47             while (!s2.empty())
     48             {
     49                 s1.push(s2.top());
     50                 s2.pop();
     51             }
     52         }
     53 
     54     }
     55     //返回队列的队头元素
     56     int& Front()
     57     {
     58         int* val = new int;
     59         if (this->empty())
     60         {
     61             throw exception("MyQueue empty");
     62         }
     63         //先把元素拷进中转栈
     64         if (!s1.empty())
     65         {
     66             if (s2.empty())
     67             {
     68                 while (!s1.empty())
     69                 {
     70                     s2.push(s1.top());
     71                     s1.pop();
     72                 }
     73             }
     74         }
     75         if (!s2.empty())
     76         {
     77             *val = s2.top();
     78             while (!s2.empty())
     79             {
     80                 s1.push(s2.top());
     81                 s2.pop();
     82             }
     83         }
     84         return *val;
     85     }
     86 
     87     //返回队列的队尾元素
     88     int& Back()
     89     {
     90         
     91         if (this->empty())
     92         {
     93             throw exception("MyQueue empty");
     94             
     95         }
     96         return s1.top();
     97     }
     98 private:
     99     stack<int> s1;            //队列的尾
    100     stack<int> s2;            //队列的头
    101 };
    102 
    103 
    104 void test02()
    105 {
    106     Myqueue que;
    107     //给队列添加元素
    108     for (int i = 0; i < 10; i++)
    109     {
    110         que.push(i + 20);
    111     }
    112     //返回队头元素
    113     cout << que.Front() << endl;
    114     //返回队尾元素
    115     cout << que.Back() << endl;
    116 
    117     //删除队头元素
    118     que.Pop();
    119     que.Pop();
    120 
    121     //返回队头元素
    122     cout << que.Front() << endl;
    123     //返回队尾元素
    124     cout << que.Back() << endl;
    125     //判断队列是否为空
    126     if (que.empty())
    127     {
    128         cout << "队列为空" << endl;
    129     }
    130     else
    131     {
    132         cout << "队列不为空" << endl;
    133     }
    134 }
    135 
    136 
    137 int main02()
    138 {
    139 
    140     test02();
    141 
    142     system("pause");
    143     return EXIT_SUCCESS;
    144 }

    总结: 这两种方法的不同之处在于:

      1.第一种方法:只向中转栈容器拷贝一次,直至这个栈容器空。(即自建队列元素进入的那个栈容器);

      2.第二种方法:先向中转栈容器中拷贝,每次操作完后再将中转容器中剩余的元素拷贝回原来的栈容器(即自建队列元素进入的那个栈容器);

    困惑之处:

      1.当时看到这道题的困惑之处在于,如何把两个栈容器绑定在一起,(当时陷入死循环,忘了创建类这中方法)。

      2.当创建一个类时,用它定义的对象就可以作为自定义的队列。(也可以很好的把两个栈容器集成在一块)。

  • 相关阅读:
    bzoj 4539 [Hnoi2016]树——主席树+倍增
    bzoj 4137 [FJOI2015]火星商店问题——线段树分治+可持久化01trie树
    bzoj 4025 二分图——线段树分治+LCT
    LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治
    bzoj 3572 [Hnoi2014]世界树——虚树
    bzoj 4650(洛谷 1117) [Noi2016]优秀的拆分——枚举长度的关键点+后缀数组
    洛谷 P3957 跳房子 —— 二分答案+单调队列优化DP
    洛谷 P1578 奶牛浴场 —— 最大子矩形
    bzoj 1510 Kra-The Disks —— 思路
    bzoj 1657 Mooo 奶牛的歌声 —— 单调栈
  • 原文地址:https://www.cnblogs.com/yyx1-1/p/5778005.html
Copyright © 2011-2022 走看看