版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据结构之队列1、定义队列也是一种线性表,但就像堆栈一样,它的操作也受到限制。队列的所有插入操作都在表的一端进行,所有的删除操作都在表的另一端进行。它的所有操作都遵循“先进先出”原则,即先进入队列的元素一定是先离开队列。进行删除的一端叫做“队头”(Front),进行插入的一端叫做“队尾”(Rear)。2、队列的基本操作向队尾插入元素:QInsert()/入队一个元素删除队首元素:QDelete()/出队一个元素获取队首元素:QRead()/读取队首元素判断队列是否空:IsEmpty()判断队列是否满:IsFull()清空队列:QClear()3、队列的实现方法按照存储方式的不同队列分为两种,一
2、种采用顺序存储方式存储称为顺序队列,另一种采用链式存储方式存储称为链式队列。3.1、顺序队列1顺序队列删除队头元素有以下两种方式。(1)不要求队头元素必须存放在下标为零的数组元素中每次删除队头元素,只需修改队头指针的位置,令front=front+1。该方式的优点是无须改变其它队列元素的地址,缺点是front值随着队头元素的删除而不断增加,整个队列向数组的后端移动。随着队尾元素的不断加入,必须出现数组后端没有可用空间的情况,而数组前端的大量空间却被闲置。(2)要求队头元素必须存放在下标为零的数组元素中每次删除队头元素,令所有元素都向前移动一个位置。该方式的优点是不浪费空间,缺点是所有元素的地址
3、都必须改变,效率低下。为了克服上述缺点,可以假定数组是循环的,即采用环状模型来实现顺序队列。之和模型将队列在逻辑上置于一个圆环上,如下图所示,用整形变量front存放队头位置,每删除一个队头元素,就将front顺时针移动一个位置。整形变量rear存放新元素要插入的位置,每插入一个元素,rear将顺时针移动一个位置。整形变量count存放队列中元素的个数。当count等于数组规模size时,说明队列已满;当count等于0时,说明队列为空。环状队列的实现要用到求余运算:Front顺时针移动1位:front=(front+1)MOD sizerear顺时针移动1位:rear=(rear+1)MOD
4、 size以下是顺序队列的C+代码:/实现顺序队列#ifndef mod#define mod(x, y) (x%y)#endif#include <iostream>using namespace std;/类声明template <class T>class AQueuepublic:AQueue(int MaxQueueSize);/构造函数AQueue();/析构函数bool QInsert(const T &item);/向队尾插入元素itembool QDelete(T &item);/删除队首元素并将该元素值保存到itembool QRea
5、d(T &item)const;/读取队首元素值bool IsEmpty()const;/判断队列是否空bool IsFull()const;/判断队列是否满void QClear();/清空队列private:int front;/队首元素在数组中的位置int rear;/新元素要插入的位置int count;/队列中元素的个数int size;/队列的规模T *QArray;/存放元素的数组;/类实现template <class T>AQueue<T>:AQueue(int MaxQueueSize)front = 0;rear = 0;count = 0
6、;size = MaxQueueSize;QArray = new TMaxQueueSize;template <class T>AQueue<T>:AQueue()delete QArray;template <class T>bool AQueue<T>:QInsert(const T &item)if (IsFull()/* code */cout<<"Queue is full, can't insert data."<<endl;return false;QArrayrear
7、 = item;rear = mod(rear + 1, size);count+;return true;template <class T>bool AQueue<T>:QDelete(T &item)if (IsEmpty()/* code */cout<<"Queue is empty, can't delete."<<endl;return false;item = QArrayfront;front = mod(front + 1, size);count-;return true;template
8、 <class T>bool AQueue<T>:QRead(T &item)constif (IsEmpty()/* code */cout<<"Queue is empty, can't read."<<endl;return false;item = QArrayfront;return true;template <class T>bool AQueue<T>:IsEmpty()constreturn (count = 0);template <class T>boo
9、l AQueue<T>:IsFull()constreturn (count = size);template <class T>void AQueue<T>:QClear()front = 0;rear = 0;count = 0;3.2、链式队列采用链式存储方式实现的队列称为链式队列。链式队列也用单链表结构,限定在表头(队列头)进行出队操作,在表尾(队列尾)进行入队操作。在实现过程中需要建立结点结构,定义分别指向队头和队尾的指针front和rear。首先建立结点结构LQueueNode,如下:template <class T>struct
10、LStackNodeT data;/存储数据LStackNode<T> *next; /指向下一个结点LStackNode(LStackNode* nextNode = NULL)next = nextNode;LStackNode(const T &item, LStackNode* nextNode =NULL)data = item;next = nextNode;以下是链式队列LQueue类的C+代码:/实现链式队列#include "stdafx.h"#include <iostream>using namespace std;/建
11、立结点结构template <typename _InputIter>struct LQueueNode_InputIter data;LQueueNode<_InputIter> *next;LQueueNode(LQueueNode<_InputIter> *nextNode = NULL)next = nextNode;LQueueNode(const _InputIter &item, LQueueNode<_InputIter> *nextNode = NULL)data = item;next = nextNode;/类声明t
12、emplate <typename _InputIter>class LQueuepublic:LQueue();LQueue();bool QInsert(const _InputIter &item);bool QDelete(_InputIter &item);bool QRead(_InputIter &item)const;bool IsEmpty()const;void QClear();void QShow()const;private:LQueueNode<_InputIter> *front;LQueueNode<_Inp
13、utIter> *rear;/类实现template <typename _InputIter>LQueue<_InputIter>:LQueue()front = NULL;rear = NULL;template <typename _InputIter>LQueue<_InputIter>:LQueue()QClear();template <typename _InputIter>bool LQueue<_InputIter>:QInsert(const _InputIter &item)if (Is
14、Empty()/* code */front = new LQueueNode<_InputIter>(item);rear = front;return true;rear->next = new LQueueNode<_InputIter>(item);rear = rear->next;return true;template <typename _InputIter>bool LQueue<_InputIter>:QDelete(_InputIter &item)/注意此处容易忽略释放内存if (IsEmpty()/*
15、 code */cout << "LQueue is empty, can't delete." << endl;return false;LQueueNode<_InputIter> *q = front;item = q->data;front = front->next;delete q;/此处较容易忽略,当front=null时,rear=nullif (IsEmpty()/* code */rear = NULL;return true;template <typename _InputIter>
16、bool LQueue<_InputIter>:QRead(_InputIter &item)constif (IsEmpty()/* code */cout << "LQueue is empty, can't read." << endl;return false;item = front->data;return true;template <typename _InputIter>bool LQueue<_InputIter>:IsEmpty()constreturn (front = NULL);template <typename _InputIter>void LQueue<_InputIter>:QClear()while(!IsEmpty()LQueueNode<_InputIter> *q = front;fr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年短视频内容分发合作协议(收益分配)
- 2025年短视频内容独家合作协议
- 2025电子产品购销合同书模板
- 2025年短视频电商变现合作协议
- 2025装载机租赁合同书版
- 2025全新合同模板:员工劳动合同范例
- 2025金融居间服务合同范本
- 2025年道路运输出租车企业安全生产年终总结报告
- 员工培养协议书
- 委托支付协议书签几方协议书
- 服务机器人应用技术员职业技能竞赛理论考试题库(含答案)
- 电气工程及其自动化职业规划课件
- 陇南成县招聘司法协理员考试试卷及答案
- 生涯发展报告书模板
- GB/T 693-2024化学试剂三水合乙酸钠(乙酸钠)
- 2023年江苏省五年制专转本英语统考真题(试卷+答案)
- 上海市2024年春季高三英语统一考试试题(含解析)
- 22G101三维彩色立体图集
- (正式版)HG∕T 20644-2024 弹簧支吊架选用标准
- 数字化设计与制造课程教学大纲
- 人教版小学英语单词表(完整版)
评论
0/150
提交评论