Queues 队列

news/2024/7/7 23:44:38

1. Definiation

What is a queue?

A queue is a list. With a queue, inseration is done at one end (known as rear) whereas deletion is performed at the other end (known as front).

 

2. Operations

指针对列

无法自定义队长

// array queue
#include<iostream>
using namespace std;struct Node
{int data;
};class Queue
{
public://初始化Queue(){front = -1;rear = -1;}//判断是否为空bool isEmpty(){if (rear == front)return true;return false;}//判队满bool isFull(){if (rear - front == 10)return true;return false;}//入队列void EnQueue(int item){if (!isFull()){rear += 1;node[rear].data = item;}else{cout << "Full" << endl;}}//出队列void DiQueue(){if (!isEmpty()){front += 1;}else{cout << "Empty" << endl;}}//取队头int GetHead(){if (!isEmpty()){int temp = node[front+1].data;return temp;}else{cout << "Empty" << endl;return 0;}}//得队长int GetSize(){return rear - front;}private:int front;   //队头指针总是指向队头元素的前一个位置int rear;Node node[10];
};

可自定义队长

//自己动手写queue
//array queue
template<typename Object>
struct Node
{Object data;
};template<typename Object>
class Queue
{
public://初始化Queue(int m){init(m);}Queue&operator=(Queue&q){if (this == &q)return *this;clear();for (int i = 0; i < q.curSize; i++)this->EnQueue(q.list[i].data);return *this;}Queue(Queue&q){init(q.maxsize);*this = q;}//删除队列~Queue(){delete list;}//判断是否为空bool isEmpty(){if (curSize == 0)return true;return false;}//判队满bool isFull(){if (curSize == maxsize)return true;return false;}//入队列void EnQueue(int item){if (!isFull()){rear += 1;list[rear].data = item;curSize++;}else{cout << "Full" << endl;}}//出队列void DiQueue(){if (!isEmpty()){front += 1;curSize--;}else{cout << "Empty" << endl;}}//取队头int GetHead(){if (!isEmpty()){int temp = list[front + 1].data;return temp;}else{cout << "Empty" << endl;return 0;}}//得队长int GetSize(){return curSize;}//清空队列void clear(){for (int i = 0; i<curSize; i++){if (isEmpty()){return;}else{rear--;}}curSize = 0;}private:int front;int rear;int maxsize;int curSize;Node<Object>* list;void init(int m){front = -1;rear = -1;maxsize = m;curSize = 0;list = new Node<Object>[maxsize];}
};

  

 

 

一般的数组队列会出现假溢出的问题,为了解决这个问题,我们可以利用循环队列

需要注意的是,你设计的队列大小与实际可以用到的队列大小是不一样的,总是-1

// circular queue
#include<iostream>
using namespace std;struct Node
{int data;
};class CirQueue
{
public://初始化CirQueue(){rear = 3;front = 3;}//判空bool isEmpty(){if (front == rear)return true;return false;}//判满,牺牲队列的一个空间来判满bool isFull(){if ((rear + 1) % 4 == front)return true;return false;}//入队void EnQueue(int item){if (!isFull()){rear = (rear + 1) % 4;node[rear].data = item;}else{cout << "Full" << endl;}}//出队void DiQueue(){if (!isEmpty()){front = (front + 1) % 4;}else{cout << "Empty" << endl;}}//取队头int GetHead(){if (!isEmpty()){return node[(front+1)%4].data;}else return -1;}
private:int front;int rear;Node node[4];
};

其实不用游标来判断队空与队满就不用牺牲一个空间了

//自己动手写queue
//circular queue
template<typename Object>
struct Node
{Object data;Node(const Object &d = Object()) :data(d){}
};template<typename Object>
class CircularQueue
{
public://the big threeCircularQueue(int m){init(m);}CircularQueue(CircularQueue& cq){init(cq.maxSize);*this = cq;}~CircularQueue(){delete[] node;}CircularQueue& operator=(CircularQueue &cq){if (this == &cq)return *this;clear();for (int i = 0; i<cq.curSize; i++)this->EnQueue(cq.node[i].data);return *this;}//判空bool isEmpty(){return curSize == 0;}//判满bool isFull(){return curSize == maxSize;}//入队void EnQueue(Object item){if (!isFull()){curSize++;}rear = (rear + 1) % maxSize;node[rear].data = item;}//出队void DIQueue(){if (!isEmpty()){head = (head + 1) % maxSize;curSize--;}}//取队头Object GetHead(){return node[(head + 1) % maxSize].data;}//得队长int GetSize(){return curSize;}//清空队列void clear(){head = maxSize - 1;rear = maxSize - 1;curSize = 0;}private:int head;int rear;int maxSize;int curSize;Node<Object>*node;void init(int m){head = m - 1;rear = m - 1;maxSize = m;curSize = 0;node = new Node<Object>[maxSize];}
};

  

  

 

 

链表队列

就无什么假溢出的问题了

template<typename Type>
struct Node
{Type data;Node*next;Node*prev;Node(const Type& d = Type(), Node*p = NULL, Node *n = NULL) :data(d), prev(p), next(n){}
};template<typename Type>
class Queue
{
public:Queue(){init();}Queue(Queue& q){init();*this = q;}~Queue(){clear();delete head;delete tail;}const Queue& operator= (const Queue& q){if (this == &q)return *this;clear();Node<Type>*p = q.head->next;while (p->next != q.tail){this.EnQueue(p->data);p = p->next;}return *this;*/}bool IsEmpty(){return size == 0;}void EnQueue(Type item){Node<Type>*p = new Node<Type>;p->data = item;p->prev = tail->prev;tail->prev->next = p;tail->prev = p;p->next = tail;size++;}void DIQueue(){if (!IsEmpty()){Node<Type>*p = head->next;head->next = p->next;p->next->prev = head;delete p;size--;}}Type GetHead(){return (head->next->data);}void clear(){while (head->next != tail){DIQueue();}size = 0;}int GetSize(){return size;}private:Node<Type>*tail;Node<Type>*head;int size;void init(){head = new Node<Type>;tail = new Node<Type>;head->next = tail;tail->prev = head;size = 0;}
};

 

转载于:https://www.cnblogs.com/KennyRom/p/5944443.html


http://lihuaxi.xjx100.cn/news/242251.html

相关文章

精通Python网络爬虫:核心技术、框架与项目实战.1.1 初识网络爬虫

摘要 网络爬虫也叫做网络机器人&#xff0c;可以代替人们自动地在互联网中进行数据信息的采集与整理。在大数据时代&#xff0c;信息的采集是一项重要的工作&#xff0c;如果单纯靠人力进行信息采集&#xff0c;不仅低效繁琐&#xff0c;搜集的成本也会提高。此时&#xff0c;我…

C#代码实现对Windows凭据的管理

今天有个任务&#xff0c;那就是使用C#代码实现对windows凭据管理的操作。例如&#xff1a;向windows凭据管理中添加凭据、删除凭据以及查询凭据等功能。于是乎&#xff0c;就开始在网上查找。经过漫长的查询路&#xff0c;终于在一片英文博客中找到了相关代码。经过实验&#…

内科学与计算机专业的相关性,急性心肌梗死患者QT间期变异性及心率变异性与室性心律失常的相关性分析-内科学专业论文.docx...

苏州大学学位论文使用授权声明本人完全了解苏州大学关于收集、保存和使用学位论文的规定&#xff0c;苏州大学学位论文使用授权声明本人完全了解苏州大学关于收集、保存和使用学位论文的规定&#xff0c; 即&#xff1a;学位论文著作权归属苏州大学。本学位论文电子文档的内容和…

顶级数据库管理系统的性能比较研究(论文翻译)

本文译自 《A Comparative Study on the Performance of the Top DBMS Systems 》Youssef Bassil LACSC – Lebanese Association for Computational Sciences Registered under No. 957, 2011, Beirut, Lebanon 摘要 数据库管理系统是当今将数据组织成可以搜索和更新的集合地…

女生参加软件测试培训合适吗

女生参加软件测试培训合适吗?这个问题困扰着很多女性朋友&#xff0c;大部分女性觉得软件测试属于IT技术行业&#xff0c;学起来是比较麻烦的&#xff0c;不知道是否适合女性&#xff0c;我们来看看下面的详细介绍。 女生参加软件测试培训合适吗?当然合适&#xff0c;如果说要…

《Ansible权威指南 》一 第一篇 Part 1 基础入门篇

本节书摘来自华章出版社《Ansible权威指南 》一书中的第1章&#xff0c;第1.1节&#xff0c;李松涛 魏 巍 甘 捷 著更多章节内容可以访问云栖社区“华章计算机”公众号查看。 第一篇 Part 1 基础入门篇第1章 Ansible基础入门第2章 Ansible基础元素介绍第3章 Ansible Ad…

全国计算机等级考试二级vb上机模拟软件,(全国计算机等级考试二级Vb上机模拟9-13.doc...

全国计算机等级考试二级Vb上机模拟(9)在考生文件夹下&#xff0c;完成如下操作&#xff1a;(1)建立数据库ordersmanage.dbc&#xff0c;把自由表employee.dbf和orders.dbf添加到数据库中。(2)打开表单dh.scx&#xff0c;设置标签控件中英文字母的字号为18&#xff0c;保存表单。…

2016.8.11 DataTable合并及排除重复方法

合并&#xff1a; DataTable prosxxx; DataTable pstaryyy; //将两张DataTable合成一张 foreach (DataRow dr in pstar.Rows) { pros.ImportRow(dr); } DataTable设置主键&#xff0c;并判断重复 DataTable allpros xxx; 单列设为主键&#xff1a; //设置第某列为主键 allpros.…