C++栈和队列怎么实现

发布时间:2021-11-30 15:52:54 作者:iii
来源:亿速云 阅读:115

本篇内容主要讲解“C++栈和队列怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++栈和队列怎么实现”吧!

栈的定义和实现

#ifndef Stack_H  #define Stack_H   #include "List.h"   template <class Type> 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 Type> 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<int> 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<int> 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++栈和队列怎么实现”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Python实现栈和队列的代码
  2. 关于栈和队列的相关问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:Flex如何动态读取外部XML文件

下一篇:C/C++ Qt TreeWidget单层树形组件怎么应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》