在C++数据结构学习中,顺序表示的栈和队列,必须预先分配空间,并且空间大小受限,使用起来限制比较多。而且,由于限定存取位置,顺序表示的随机存取的优点就没有了,所以,链式结构应该是***。

开平网站建设公司成都创新互联,开平网站设计制作,有大型网站制作公司丰富经验。已为开平上1000家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的开平做网站的公司定做!
栈的定义和实现
- #ifndef Stack_H
 - #define Stack_H
 - #include "List.h"
 - template
 class Stack : List //栈类定义 - {
 - public:
 - void Push(Type value)
 - {
 - Insert(value);
 - }
 - Type Pop()
 - {
 - Type p = *GetNext();
 - RemoveAfter();
 - return p;
 - }
 - Type GetTop()
 - {
 - return *GetNext();
 - }
 - List ::MakeEmpty;
 - List ::IsEmpty;
 - };
 - #endif
 
队列的定义和实现
- #ifndef Queue_H
 - #define Queue_H
 - #include "List.h"
 - template
 class Queue : List //队列定义 - {
 - public:
 - void EnQueue(const Type &value)
 - {
 - LastInsert(value);
 - }
 - Type DeQueue()
 - {
 - Type p = *GetNext();
 - RemoveAfter();
 - IsEmpty();
 - return p;
 - }
 - Type GetFront()
 - {
 - return *GetNext();
 - }
 - List ::MakeEmpty;
 - List ::IsEmpty;
 - };
 - #endif
 
测试程序
- #ifndef StackTest_H
 - #define StackTest_H
 - #include "Stack.h"
 - void StackTest_int()
 - {
 - cout << endl << "整型栈测试" << endl;
 - cout << endl << "构造一个空栈" << endl;
 - Stack
 a; - cout << "将1~20入栈,然后再出栈" << endl;
 - for (int i = 1; i <= 20; i++) a.Push(i);
 - while (!a.IsEmpty()) cout << a.Pop() << ' ';
 - cout << endl;
 - }
 - #endif
 - #ifndef QueueTest_H
 - #define QueueTest_H
 - #include "Queue.h"
 - void QueueTest_int()
 - {
 - cout << endl << "整型队列测试" << endl;
 - cout << endl << "构造一个空队列" << endl;
 - Queue
 a; - cout << "将1~20入队,然后再出队" << endl;
 - for (int i = 1; i <= 20; i++) a.EnQueue(i);
 - while (!a.IsEmpty()) cout << a.DeQueue() << ' ';
 - cout << endl;
 - }
 - #endif
 
没什么好说的,你可以清楚的看到,在单链表的基础上,栈和队列的实现是如此的简单。
如读者希望继续阅读栈和队列的应用,请阅读拓展文章C++数据结构学习之栈的应用和C++数据结构学习之队列的应用 。
                网页标题:C++数据结构学习之栈和队列
                
                URL标题:http://www.csdahua.cn/qtweb/news5/534205.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网