C++数据结构学习之栈和队列

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

开平网站建设公司成都创新互联,开平网站设计制作,有大型网站制作公司丰富经验。已为开平上1000家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的开平做网站的公司定做!

  栈的定义和实现

 
 
 
  1. #ifndef Stack_H
  2. #define Stack_H 
  3. #include "List.h"
  4. template  class Stack : List //栈类定义  
  5. {
  6.  public:
  7.   void Push(Type value)
  8.   {
  9.    Insert(value);
  10.   }
  11.  Type Pop()
  12.  {
  13.   Type p = *GetNext();
  14.   RemoveAfter();
  15.   return p;
  16.  }
  17.  Type GetTop()
  18.  {
  19.   return *GetNext();
  20.  }
  21.  List  ::MakeEmpty;  
  22.  List  ::IsEmpty;  
  23. };
  24. #endif

  队列的定义和实现

 
 
 
  1. #ifndef Queue_H
  2. #define Queue_H
  3. #include "List.h"
  4. template  class Queue : List //队列定义  
  5. {
  6.  public:
  7.   void EnQueue(const Type &value)
  8.   {
  9.    LastInsert(value);
  10.   }
  11.  Type DeQueue()
  12.  { 
  13.   Type p = *GetNext();
  14.   RemoveAfter();
  15.   IsEmpty();
  16.   return p;
  17.  }
  18.  Type GetFront()
  19.  {
  20.   return *GetNext();
  21.  }
  22.  List  ::MakeEmpty;  
  23.  List  ::IsEmpty;  
  24. };
  25. #endif

  测试程序

 
 
 
  1. #ifndef StackTest_H
  2. #define StackTest_H
  3. #include "Stack.h"
  4. void StackTest_int()
  5. {
  6.  cout << endl << "整型栈测试" << endl;
  7.  cout << endl << "构造一个空栈" << endl;
  8.  Stack a;
  9.  cout << "将1~20入栈,然后再出栈" << endl;
  10.  for (int i = 1; i <= 20; i++) a.Push(i);
  11.   while (!a.IsEmpty()) cout << a.Pop() << ' ';
  12.   cout << endl;
  13. }
  14. #endif
  15. #ifndef QueueTest_H
  16. #define QueueTest_H
  17. #include "Queue.h"
  18. void QueueTest_int()
  19. {
  20.  cout << endl << "整型队列测试" << endl;
  21.  cout << endl << "构造一个空队列" << endl;
  22.  Queue a;
  23.  cout << "将1~20入队,然后再出队" << endl;
  24.  for (int i = 1; i <= 20; i++) a.EnQueue(i);
  25.  while (!a.IsEmpty()) cout << a.DeQueue() << ' ';
  26.  cout << endl;
  27. }
  28. #endif

  没什么好说的,你可以清楚的看到,在单链表的基础上,栈和队列的实现是如此的简单。

  如读者希望继续阅读栈和队列的应用,请阅读拓展文章C++数据结构学习之栈的应用和C++数据结构学习之队列的应用 。

标题名称:C++数据结构学习之栈和队列
本文来源:http://www.hantingmc.com/qtweb/news5/534205.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联