zoukankan      html  css  js  c++  java
  • 海涛老师的面试题作业7栈与队列之间的转换

    View Code
      1 // 栈变队列变栈.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 /****************************************************
      5 
      6 设计者:cslave
      7 版本说明:本代码免费用于商用,拷贝,转移,使用,但是
      8 有本代码导致的问题,本人概不负责。
      9 设计时间:2012.6.25
     10 分发原则:遵守GNU规范。
     11 
     12 题目:使用两个队列来实现栈,或者使用两个栈来实现队列。
     13 
     14 首先讲讲两个栈来实现队列,这个比较简单,队列先进先出,
     15 栈后进先出,先在一个栈A尾部添加元素,需要删除的时候
     16 将一个栈A元素全部出队列放入另一个栈B,然后再有添加
     17 元素的话放在本栈A,有删除就从栈B中出,当B为空时,直到
     18 B为空,再将A中元素给B。如此翻转。
     19 
     20 
     21 在讲讲两个队列实现栈,先在一个空队列 A push元素,需要pop的
     22 时候,将A的元素入队列B,直到A中有一个元素,即为该元素,然后
     23 pop掉,再次push的话,放入队列B中,一次类推。
     24 
     25 *****************************************************/
     26 
     27 
     28 
     29 #include "stdafx.h"
     30 #include <stack>
     31 #include <queue>
     32 #include <iostream>
     33 using namespace std;
     34 
     35 template<typename T>
     36 class CQueue            //队列模板
     37 {
     38 public:
     39     CQueue(void);
     40     ~CQueue(void);
     41     void AppendTail(const T& node);//队列的尾部加入函数
     42     T    DeleteHead();
     43 private:
     44     stack<T> stack1;
     45     stack<T> stack2;
     46 };
     47 
     48 
     49 
     50 template<typename T>
     51 CQueue<T>::CQueue()
     52 {
     53 
     54 }
     55 
     56 template<typename T>
     57 CQueue<T>::~CQueue()
     58 {
     59 
     60 }
     61 
     62 template<typename T>
     63 void CQueue<T>::AppendTail(const T& node) //队列尾部添加函数
     64 {
     65     stack1.push(node);
     66 }
     67 
     68 template<typename T>
     69 T CQueue<T>::DeleteHead()
     70 {
     71     if(stack2.size()<=0)  // 栈2 队列当有出元素操作时,若栈2为空 则栈1元素入栈2,然后从栈2出队列
     72     {
     73         while(stack1.size()>0)
     74         {
     75             T& Data=stack1.top();
     76             stack1.pop();
     77             stack2.push(Data);
     78         }
     79     }
     80     if(stack2.size()<=0)
     81         throw  new exception("queue is empty!");
     82     T Head=stack2.top();
     83     stack2.pop();
     84     return Head;
     85 }
     86 
     87 
     88 
     89 template<typename T>
     90 class CStack
     91 {
     92 public:
     93     CStack();
     94     ~CStack();
     95     void PushBack(const T& node);
     96     T    Top();
     97     T    Pop();
     98 private:
     99     queue<T> queue1;
    100     queue<T> queue2;
    101 };
    102 
    103 template<typename T>
    104 CStack<T>::CStack()
    105 {
    106 
    107 }
    108 
    109 template<typename  T>
    110 CStack<T>::~CStack()
    111 {
    112 
    113 }
    114 
    115 template<typename T>
    116 void CStack<T>::PushBack(const T& node)  //栈的PUSH函数,因为每次出栈都可能导致两队列间元素转移,所以要判断
    117 {
    118     if(!queue1.empty())  
    119     {
    120         queue1.push(node);
    121     }
    122     else if(!queue1.empty())
    123     {
    124         queue2.push(node);
    125     }
    126     else
    127     {
    128         queue1.push(node);
    129     }
    130 }
    131 
    132 template<typename T>
    133 T CStack<T>::Top()//这个函数有点投机取巧了,不能算。
    134 {
    135     if(!queue1.empty())
    136         return queue1.back();
    137     else if(!queue2.empty())
    138         return queue2.back();
    139     else
    140         throw new exception("Invalid Stack");
    141 }
    142 
    143 template<typename T>
    144 T CStack<T>::Pop()  //出栈操作,比较麻烦,o(n)复杂度
    145 {
    146     T node;
    147     if(!queue1.empty())
    148     {
    149         while(queue1.size()!=1)
    150         {
    151             node=queue1.front();
    152             queue2.push(node);
    153             queue1.pop();
    154         }
    155         node=queue1.front();
    156         queue1.pop();
    157         return node;
    158     }
    159     else if(!queue2.empty())
    160     {
    161         while(queue2.size()!=1)
    162         {
    163             node=queue2.front();
    164             queue1.push(node);
    165             queue2.pop();
    166         }
    167         node=queue2.front();
    168         queue1.pop();
    169         return node;
    170     }
    171     else
    172         throw new exception("Invalid Stack!");
    173 }
    174 
    175 
    176 void Test(char actual, char expected)
    177 {
    178     if(actual == expected)
    179         printf("Test passed.\n");
    180     else
    181         printf("Test failed.\n");
    182 }
    183 
    184 void TestStack(char actual,char expected)
    185 {
    186     if(actual==expected)
    187         printf("Test passed.\n");
    188     else
    189         printf("Test failed.\n");
    190 }
    191 
    192 
    193 
    194 int _tmain(int argc, _TCHAR* argv[])
    195 {
    196        
    197     cout<<"队列测试开始!"<<endl;
    198     CQueue<char> queue;
    199     queue.AppendTail('a');
    200     queue.AppendTail('b');
    201     queue.AppendTail('c');
    202 
    203     char head = queue.DeleteHead();
    204     Test(head, 'a');
    205 
    206     head = queue.DeleteHead();
    207     Test(head, 'b');
    208 
    209     queue.AppendTail('d');
    210     head = queue.DeleteHead();
    211     Test(head, 'c');
    212 
    213     queue.AppendTail('e');
    214     head = queue.DeleteHead();
    215     Test(head, 'd');
    216 
    217     head = queue.DeleteHead();
    218     Test(head, 'e');
    219 
    220     cout<<"栈测试开始!"<<endl;
    221     CStack<char> stack;
    222     stack.PushBack('a');
    223     stack.PushBack('b');
    224     stack.PushBack('c');
    225     char front=stack.Top();
    226     TestStack(front,'c');
    227     stack.Pop();
    228     front=stack.Top();
    229     TestStack(front,'b');
    230     stack.Pop();
    231     stack.PushBack('d');
    232     front=stack.Top();
    233     TestStack(front,'d');
    234     stack.Pop();
    235     stack.PushBack('e');
    236     front=stack.Top();
    237     TestStack(front,'e');
    238     stack.Pop();
    239     return 0;
    240 }
  • 相关阅读:
    Difference between application/xml and text/xml
    [转]ASP.NET MVC URL 路由简介
    js Unicode编码转换
    svn更改用户名
    招商银行开发成功国内业界第一个实用的数据仓库系统
    Response.ContentType 详细列表备忘使用(转载)
    WebClient UploadData UploadFile 用法
    char tchar wchar_t WCHAR LPCTSTR LPCWSTR
    网站测试自动化系统—系统应该有的功能
    网站测试自动化系统—收集测试结果
  • 原文地址:https://www.cnblogs.com/cslave/p/2564183.html
Copyright © 2011-2022 走看看