两个栈实现一个队列

2013-08-18 18:59:11

两个栈实现一个队列,包括队列的初始化、销毁、入队、出队、测长、判断空否、显示队列元素等操作。

小结:

  1. 用两个栈作为队列结构,一个用于入队,另一个作为出队的辅助栈,如代码中栈sEnQueue、sDeQueue为队列结构的两个成员;
  2. 初始化队列,即两个栈的初始化,如代码中函数InitQueue;
  3. 队列销毁,即两个栈的销毁,如代码中函数DestoryQueue;
  4. 出队,若sDeQueue为空,则将sEnQueue的元素倒入sDeQueue,然后取其栈顶元素;若非空,直接取其栈顶元素;
  5. 入队,即sEnQueue的入栈操作;
  6. 队列测长,即两个栈的长度值之和;
  7. 注意队列的显示总,两个栈的显示顺序不同,才能得到正确的显示。

 


 

代码(测试暂未发现错误,欢迎交流指正):

使用的"link_stack.h"头文件:

  1 #include <iostream>
  2 #include <cassert>
  3 
  4 #ifndef _LINK_STACK_H
  5 #define _LINK_STACK_H
  6 
  7 using namespace std;
  8 
  9 typedef int DataType; 
 10 
 11 typedef struct node
 12 {
 13     DataType data;
 14     struct node *next;
 15 }LNode,*PLNode;
 16 
 17 typedef struct stack
 18 {
 19     PLNode top;
 20 }Stack;
 21 
 22 //初始化栈
 23 void InitStack(Stack &s)
 24 {
 25     s.top = NULL;  //不需头结点
 26 }
 27 
 28 //初始化栈
 29 void DestoryStack(Stack &s)
 30 {
 31     PLNode pPre = s.top;
 32     PLNode pCur = pPre->next;
 33     while (NULL != pCur)
 34     {
 35         delete pPre;
 36         pPre = pCur;
 37         pCur = pCur->next;
 38     }
 39 }
 40 
 41 //出栈
 42 void PushStack(Stack &s,DataType data)
 43 {
 44     PLNode pNew = new LNode;
 45     pNew->data = data;
 46 
 47     pNew->next = s.top;
 48     s.top = pNew;
 49 }
 50 
 51 //出栈
 52 DataType PopStack(Stack &s)
 53 {
 54     assert(NULL != s.top);
 55 
 56     DataType topData = s.top->data;
 57 
 58     PLNode headToDelete = s.top;
 59     s.top = s.top->next;
 60     delete headToDelete;
 61 
 62     return topData;
 63 }
 64 
 65 //获取栈顶元素
 66 DataType GetTopOfStack(Stack &s)
 67 {
 68     assert(NULL != s.top);
 69     return ( s.top->data );
 70 }
 71 
 72 //获取栈中元素个数
 73 size_t GetLengthOfStack(Stack &s)
 74 {
 75     size_t lengthOfStack = 0;
 76     PLNode pCur = s.top;
 77     while (NULL != pCur)
 78     {
 79         ++lengthOfStack;
 80         pCur = pCur->next;
 81     }
 82 
 83     return lengthOfStack;
 84 }
 85 
 86 //测试栈是否为空
 87 bool IsStackEmpty(Stack &s)
 88 {
 89     return (s.top == NULL ? true : false);
 90 }
 91 
 92 //从栈顶到栈底显示元素
 93 void DisplayStack(Stack &s)
 94 {
 95     PLNode pCur = s.top;
 96     while (NULL != pCur)
 97     {
 98         cout<<pCur->data<<"\t";
 99         pCur = pCur->next;
100     }
101     //cout<<endl;
102 }
103 
104 #endif

 

两个栈实现的队列:

  1 #include <iostream>
  2 #include <cassert>
  3 #include "link_stack.h"
  4 
  5 using namespace std;
  6 
  7 //用两个栈作为队列结构
  8 typedef struct queue
  9 {
 10     Stack sEnQueue;
 11     Stack sDeQueue;
 12 }Queue;
 13 
 14 //初始化队列,即两个栈的初始化
 15 void InitQueue(Queue &q)
 16 {
 17     InitStack(q.sEnQueue);
 18     InitStack(q.sDeQueue);
 19 }
 20 
 21 //队列销毁,即两个栈的销毁
 22 void DestoryQueue(Queue &q)
 23 {
 24     DestoryStack(q.sEnQueue);
 25     DestoryStack(q.sDeQueue);
 26 }
 27 
 28 //判断队列空否
 29 bool IsQueueEmpty(Queue &q)
 30 {
 31     return ( IsStackEmpty(q.sEnQueue) && IsStackEmpty(q.sDeQueue));
 32 }
 33 
 34 //出队
 35 DataType DeQueue(Queue &q)
 36 {
 37     assert( !IsQueueEmpty(q) );  //检查是否为空
 38 
 39     DataType popData = 0;
 40 
 41     if ( IsStackEmpty(q.sDeQueue) )
 42     {
 43         while ( !IsStackEmpty(q.sEnQueue) )
 44         {
 45             popData = PopStack(q.sEnQueue);
 46             PushStack(q.sDeQueue,popData);
 47         }
 48     }
 49 
 50     return PopStack(q.sDeQueue);
 51 }
 52 
 53 //入队
 54 void EnQueue(Queue &q,DataType data)
 55 {
 56     PushStack(q.sEnQueue,data);
 57 }
 58 
 59 //对列测长
 60 size_t GetLengthOfQueue(Queue &q)
 61 {
 62     return (GetLengthOfStack(q.sEnQueue) + GetLengthOfStack(q.sDeQueue));
 63 }
 64 
 65 //获取队列首元素
 66 DataType GetHeadOfQueue(Queue &q)
 67 {
 68     return ( GetTopOfStack(q.sDeQueue) );
 69 }
 70 
 71 //从栈顶到栈底显示栈中元素
 72 //pSatckTop为s.top;
 73 void DisplayReverseStack(PLNode pSatckTop)
 74 {
 75     if (NULL == pSatckTop)
 76     {
 77         return;
 78     }
 79 
 80     DisplayReverseStack(pSatckTop->next);
 81 
 82     cout<<pSatckTop->data<<"\t";
 83 }
 84 
 85 //按照入队顺序(先进先显示)显示队列元素
 86 void DisplayQueue(Queue &q)
 87 {
 88     //DisplayReverseStack(q.sDeQueue.top);
 89     DisplayStack(q.sDeQueue);
 90     DisplayReverseStack(q.sEnQueue.top);
 91     cout<<endl;
 92 }
 93 
 94 //队列测试
 95 void TestQueue()
 96 {
 97     Queue q;
 98     InitQueue(q);
 99     size_t command = 0;
100 
101     const size_t PUSH = 0;
102     const size_t POP = 1;
103     const size_t GETLENGTH = 2;
104     const size_t GETHEAD = 3;
105     const size_t ISEMPTY = 4;
106 
107     DataType data;
108 
109     cout<<"Please enter the command ,end with ctrl+z : "<<endl;
110     while (cin>>command)
111     {
112         switch(command)
113         {
114         case(PUSH):
115             {
116                 cout<<"Please enter z data to push : "<<endl;
117                 cin>>data;
118                 cout<<"enqueue "<<data<<endl;
119 
120                 cout<<"the queue before enqueue : "<<endl;
121                 DisplayQueue(q);
122 
123                 EnQueue(q,data);
124 
125                 cout<<"the queue after enqueue : "<<endl;
126                 DisplayQueue(q);
127 
128                 break;
129             }
130 
131         case(POP):
132             {
133                 cout<<"the queue before dequeue : "<<endl;
134                 DisplayQueue(q);
135 
136                 DeQueue(q);
137 
138                 cout<<"the queue after dequeue : "<<endl;
139                 DisplayQueue(q);
140 
141                 break;
142             }
143 
144         case(GETLENGTH):
145             {
146                 cout<<"the length of queue is : "<<GetLengthOfQueue(q)<<endl;
147 
148                 break;
149             }
150 
151         case(GETHEAD):
152             {
153                 cout<<"the top element of stack is : "<<GetHeadOfQueue(q)<<endl;
154 
155                 break;
156             }
157         case(ISEMPTY):
158             {
159                 cout<<"the queue is empty or not: "<<IsQueueEmpty(q)<<endl;
160 
161                 break;
162             }
163         default:
164             break;
165         }
166         cout<<"Please enter the command ,end with ctrl+z : "<<endl;
167     }
168 
169     DestoryQueue(q); //销毁栈
170 }
171 
172 int main()
173 {
174     TestQueue();
175     return 0;
176 }

 

 

测试结果:

 1 Please enter the command ,end with ctrl+z :
 2 2
 3 the length of queue is : 0
 4 Please enter the command ,end with ctrl+z :
 5 0
 6 Please enter z data to push :
 7 1
 8 enqueue 1
 9 the queue before enqueue :
10 
11 the queue after enqueue :
12 1
13 Please enter the command ,end with ctrl+z :
14 0
15 Please enter z data to push :
16 2
17 enqueue 2
18 the queue before enqueue :
19 1
20 the queue after enqueue :
21 1       2
22 Please enter the command ,end with ctrl+z :
23 0
24 Please enter z data to push :
25 3
26 enqueue 3
27 the queue before enqueue :
28 1       2
29 the queue after enqueue :
30 1       2       3
31 Please enter the command ,end with ctrl+z :
32 1
33 the queue before dequeue :
34 1       2       3
35 the queue after dequeue :
36 2       3
37 Please enter the command ,end with ctrl+z :
38 0
39 Please enter z data to push :
40 4
41 enqueue 4
42 the queue before enqueue :
43 2       3
44 the queue after enqueue :
45 2       3       4
46 Please enter the command ,end with ctrl+z :
47 0
48 Please enter z data to push :
49 5
50 enqueue 5
51 the queue before enqueue :
52 2       3       4
53 the queue after enqueue :
54 2       3       4       5
55 Please enter the command ,end with ctrl+z :
56 1
57 the queue before dequeue :
58 2       3       4       5
59 the queue after dequeue :
60 3       4       5
61 Please enter the command ,end with ctrl+z :
62 1
63 the queue before dequeue :
64 3       4       5
65 the queue after dequeue :
66 4       5
67 Please enter the command ,end with ctrl+z :
68 1
69 the queue before dequeue :
70 4       5
71 the queue after dequeue :
72 5
73 Please enter the command ,end with ctrl+z :
74 1
75 the queue before dequeue :
76 5
77 the queue after dequeue :
78 
79 Please enter the command ,end with ctrl+z :
80 1
81 the queue before dequeue :
82 
83 Assertion failed: !IsQueueEmpty(q), file e:\visual studio 2010_projects\queue_ma
84 de_by_two_stack_2013_08_17\queue_made_by_two_stack_2013_08_17\queue_made_by_two_
85 stack.cpp, line 37
86 请按任意键继续. . .

 

 

posted @ 2013-08-18 19:11  永不止步,永无止境  阅读(407)  评论(0编辑  收藏  举报