数据结构——链式队列(c++)
来源:互联网 发布:魔力宝贝热砂数据 编辑:程序博客网 时间:2024/05/19 18:15
Link.h
//Link.htemplate <class T> class Link { public: T data; // 用于保存结点元素的内容 Link * next; // 指向后继结点的指针 Link(const T info, Link* nextValue = NULL) { // 具有两个参数的Link构造函数 data = info; next = nextValue; } Link(Link* nextValue = NULL) { // 具有一个参数的Link构造函数 next = nextValue; } };
myQueue.h
//myQueue.htemplate <class T> class Queue { public: // 队列的运算集 void clear(); // 变为空队列 bool enQueue(const T item); // item入队,插入队尾,成功则返回真否则返回假 bool deQueue(T item); // 返回队头元素并从队列中删除,成功则返回真 bool front(T* item); // 返回队头元素,但不删除,成功则返回真 bool isEmpty(); // 返回真,若队列已空 bool isFull(); // 返回真,若队列已满 void print(); };
lnkQueue.h
//lnkQueue.h#include <cstdlib> #include <iostream> #include "Link.h" #include "myQueue.h" using namespace std; template <class T> class lnkQueue: public Queue <T> { private: int size; // 队列中当前元素的个数 Link<T>* front; // 表示队头的指针 Link<T>* rear; // 表示队尾的指针 public: // 队列的运算集 lnkQueue() { // 创建队列的实例 size = 0; front = rear = NULL; } ~lnkQueue() { // 消除该实例,并释放其空间 clear(); } void print() { // 打印队列 if (front == NULL) { cout << "队列为空" << endl; } Link<T>*p = front; while(p != NULL) { cout << p->data << " "; p = p->next; } cout << endl; } void clear(); // 清空队列 bool enQueue(const T item); // item入队,插入队尾 bool deQueue(T* item); // 返回队头元素并从队列中删除。注意:队列为空,没有元素可出队 bool getFront(T* item); // 返回队头元素,但不删除。注意:队列为空时,没有元素可读。item为引用亦可}; template<class T>void lnkQueue<T>::clear(){ while(front!=NULL) { rear=front; front=front->next; delete rear; } rear=NULL; size=0;}template<class T>bool lnkQueue<T>::enQueue(const T item){ if(rear==NULL) //空队列 { front=rear=new Link<T>(item,NULL); } else { rear->next=new Link<T>(item,NULL); rear=rear->next; } size++; return true;}template<class T>bool lnkQueue<T>::deQueue(T * item){ Link<T> * tmp; if(size==0) { cout<<"队列为空"<<endl; return false; } *item=front->data; tmp=front; front=front->next; delete tmp; if(front==NULL) { rear=NULL; } size--; return true;}template<class T>bool lnkQueue<T>::getFront(T * item){ if(size==0) { cout<<"队列为空"<<endl; return false; } *item=front->data; return true;}
main.cpp
//main.cpp#include <cstdlib> #include <iostream> #include "lnkQueue.h" using namespace std; int main(int argc, char *argv[]) { lnkQueue<int> Qu; for(int i=1;i<=5;i++) { Qu.enQueue(i); } cout<<"输出元素:"; Qu.print(); cout<<"入队操作:"<<endl; Qu.enQueue(8); cout<<"输出元素:"; Qu.print(); cout<<"出队操作"; int del; Qu.deQueue(&del); cout<<"出队元素为:"<<del<<endl; cout<<"输出元素:"; Qu.print();}
0 0
- 数据结构——链式队列(c++)
- 数据结构——队列的链式实现(C语言)
- C++——数据结构之 顺序队列 链式队列?
- 链式队列(数据结构C#)
- 数据结构——链式队列
- 数据结构实现链式队列(C语言)
- 数据结构(严蔚敏)之六——链式队列c语言实现
- 数据结构——链式栈(c++)
- 数据结构——队列之链式存储
- 数据结构(五)——双链表、链式栈、链式队列 及实现
- 数据结构(三)——双链表、链式栈、链式队列 及实现
- c语言版数据结构(奇迹冬瓜)-单链式队列
- 数据结构与算法(C语言版)__链式队列
- 数据结构队列(链式实现)
- 第七周项目二C/C++数据结构之自建算法库——链队(链式队列)
- 数据结构的C实现_链式队列
- 数据结构之---C语言实现链式队列
- c语言数据结构实现-链式队列
- 嗨!程序员准备好了吗?
- Java开发之设计模式--代理模式(Proxy Pattern)
- Oracle DB 12c新特性:raw device 裸设备不再受支持
- Linux调节亮度
- js加载器动态加载外部Javascript文件
- 数据结构——链式队列(c++)
- 自定义消息 + 在状态栏走进度条
- Flash制作
- js中this的指向
- VS2013常用快捷键
- 对程序员的一些建议
- CDO.Message 发邮件
- 动漫人物之Aldnoah Zero:アセイラム・ヴァース・アリューシア 艾瑟依拉姆·薇瑟·艾莉欧斯亚 CV 雨宫天
- Ubuntu 下安装java