数据结构——环形队列的原理(模拟环形队列)
知识点简要介绍:
队列:一种特殊的线性表,包含队列头、队列尾,只允许在队列头进行删除操作,在队列为进行删除操作
分类:
    顺序队列、循环队列(环形队列)
顺序队列:
    在内存中是一段连续的存储空间,并有队列头指针和队列尾指针,打个比喻吧:
    顺序队列就像在排队买车票,排在最前面(第一个人)的就是队头,排在最后的就是队尾,第一个买完票,离开(FIFO,先进先出,先排队的先走),第二个人变成了队头,整体队列前进,每走一个人,整个队列都要前进,可见整体性能不会多么的好
环形队列:
    可以将队列空间想象成一个环形空间,这能使队列空间重复使用:无论插入还是删除,front(队头)指针增1或front(队尾)指针加1时超出所分配的空间,就让它指向这片连续空间的起始地址
FIFO(先进先出)
取元素时先从队列头开始,取完后,下一个元素就成了队列头,依次循环
只有一个元素时,既是队列头,也是队列尾

--环形队列的实现原理(模拟队列的实现方式)
入队算法:
    头指针head、尾指针tail
    1.tail++;
    2.若tail = n + 1; 则tail = 1;
    3.若head = tail, 即头尾指针重合,队列已满 或 队列只有一个元素;
    4.初始化队列、入队、出队、读队、判空、判满
模拟开始:

MyQueue.h

#ifndef MY_QUEUE
#define MY_QUEUE//-----------------
class MyQueue{
public:MyQueue(int queueCapacity);//创建队列的容量virtual ~MyQueue();//销毁队列void clearQueue();//清空队列bool queueEmpty() const;//判空bool queueFull() const;//判满int queueLength() const;//长度bool enQueue(int element);//入队bool dequeue(int &element);//首元素出队void queueTraverse();//遍历队列
private:int *m_pQueue;//队列数组指针int m_iQueueLen;//个数int m_iQueueCapacity;//容量int m_iHead;int m_iTail;};#endif MY_QUEUE

MyQueue.cpp

#include"MyQueue.h"
#include<iostream>
using namespace std;//---------------------------------
MyQueue::MyQueue(int queueCapacity){m_iQueueCapacity = queueCapacity;m_iHead = 0;m_iTail = 0;m_iQueueLen = 0;m_pQueue = new int[m_iQueueCapacity];
}
MyQueue::~MyQueue(){delete []m_pQueue;m_pQueue = NULL;
}
void MyQueue::clearQueue(){m_iHead = 0;m_iTail = 0;m_iQueueLen = 0;
}
bool MyQueue::queueEmpty() const{return m_iQueueLen == 0 ? true : false;
}
bool MyQueue::queueFull() const{return m_iQueueLen == m_iQueueCapacity ? true : false;
}
int MyQueue::queueLength() const{return m_iQueueLen;
}
bool MyQueue::enQueue(int element){if(queueFull()){return false;}else{m_pQueue[m_iTail] = element;//插入元素到队列尾m_iTail++;m_iTail = m_iTail % m_iQueueCapacity;m_iQueueLen++;return true;}
}
bool MyQueue::dequeue(int &element){if(queueEmpty()){return false;}else{element = m_pQueue[m_iHead];m_iHead++;m_iHead = m_iHead % m_iQueueCapacity;m_iQueueLen--;return true;}
}
void MyQueue::queueTraverse(){for(int i = m_iHead; i < m_iQueueLen + m_iHead; i++){//因为i的初始值是不断变化的,条件判断应该加上m_iHeadcout << m_pQueue[i % m_iQueueCapacity] << endl;}cout << endl;
}

Demo.cpp

#include"MyQueue.h"
#include<iostream>
using namespace std;
//#include<iostream>
//using namespace std;
//--------------------
int main(){MyQueue *p = new MyQueue(4);p->enQueue(30);p->enQueue(20);p->enQueue(10);p->enQueue(40);p->queueTraverse();int e = 0;p->dequeue(e);cout << e << endl;p->dequeue(e);cout << e << endl;p->clearQueue();p->queueTraverse();p->enQueue(30);p->enQueue(20);p->queueTraverse();}

//控制台运行结果:
30
20
10
40

30
20

30
20

队列可运用于任何类型之中,如,将上述代码改变一下便可将队列运用于类

Customer.h

#ifndef CUSTOMER_H
#define CUSTOMER_H
//----------------
#include<string>
//#include<iostream>
using namespace std;
//----------------
class Customer{
public:Customer(string name = "", int age = 0);void printInfo() const;
private:string m_strName;int m_iAge;
};#endif CUSTOMER_H

Customer.cpp

#include<iostream>
#include"Customer.h"
using namespace std;
//------------------
Customer::Customer(string name, int age){m_strName = name;m_iAge = age;
}
void Customer::printInfo() const{cout << "姓名:" << m_strName << endl;cout << "年龄:" << m_iAge  << endl;cout << endl;
}

MyQueue.h

#ifndef MY_QUEUE
#define MY_QUEUE//-----------------
#include"Customer.h"
//----------------
class MyQueue{
public:MyQueue(int queueCapacity);//创建队列的容量virtual ~MyQueue();//销毁队列void clearQueue();//清空队列bool queueEmpty() const;//判空bool queueFull() const;//判满int queueLength() const;//长度bool enQueue(Customer element);//入队bool dequeue(Customer &element);//首元素出队void queueTraverse();//遍历队列
private:Customer *m_pQueue;//队列数组指针int m_iQueueLen;//个数int m_iQueueCapacity;//容量int m_iHead;int m_iTail;};#endif MY_QUEUE

Myqueue.cpp

#include"MyQueue.h"
#include<iostream>
using namespace std;//---------------------------------
MyQueue::MyQueue(int queueCapacity){m_iQueueCapacity = queueCapacity;m_iHead = 0;m_iTail = 0;m_iQueueLen = 0;m_pQueue = new Customer[m_iQueueCapacity];
}
MyQueue::~MyQueue(){delete []m_pQueue;m_pQueue = NULL;
}
void MyQueue::clearQueue(){m_iHead = 0;m_iTail = 0;m_iQueueLen = 0;
}
bool MyQueue::queueEmpty() const{return m_iQueueLen == 0 ? true : false;
}
bool MyQueue::queueFull() const{return m_iQueueLen == m_iQueueCapacity ? true : false;
}
int MyQueue::queueLength() const{return m_iQueueLen;
}
bool MyQueue::enQueue(Customer element){if(queueFull()){return false;}else{m_pQueue[m_iTail] = element;//插入元素到队列尾m_iTail++;m_iTail = m_iTail % m_iQueueCapacity;m_iQueueLen++;return true;}
}
bool MyQueue::dequeue(Customer &element){if(queueEmpty()){return false;}else{element = m_pQueue[m_iHead];m_iHead++;m_iHead = m_iHead % m_iQueueCapacity;m_iQueueLen--;return true;}
}
void MyQueue::queueTraverse(){for(int i = m_iHead; i < m_iQueueLen + m_iHead; i++){//因为i的初始值是不断变化的,条件判断应该加上m_iHeadm_pQueue[i % m_iQueueCapacity].printInfo();}cout << endl;
}

Demo.cpp

#include"MyQueue.h"
#include<iostream>
using namespace std;
//#include<iostream>
//using namespace std;
//--------------------
#include"Customer.h"
//--------------------
int main(){MyQueue *p = new MyQueue(4);Customer c1("L", 20);Customer c2("C", 21);Customer c3("D", 22);p->enQueue(c1);p->enQueue(c2);p->enQueue(c3);p->queueTraverse();cout << "要删除的队列头:\n" << endl;  Customer c4("",0);p->dequeue(c4);c4.printInfo();cout << "要删除后对列:\n" << endl;p->queueTraverse();}

//控制台运行结果:
姓名:L
年龄:20

姓名:C
年龄:21

姓名:D
年龄:22

要删除的队列头:

姓名:L
年龄:20

要删除后对列:

姓名:C
年龄:21

姓名:D
年龄:22

//-----------------------------------------
可以看出队列中的元素可以是任何类型,包括基本数据类型,类类型,自定义类型等,也可以通过做成模板来适应不同的类型,对否?.......确实如此吧~

数据结构——环形队列的原理(模拟环形队列)相关推荐

  1. java环形数组_Java数组模拟环形队列

    1.假溢出 ​系统作为队列用的存储区还没有满,但队列却发生了溢出,我们把这种现象称为"假溢出". 因为队列遵从从队尾存入数据,从队头取数据,所以红框部分的空间就不能继续存入新的数据 ...

  2. 队列化栈栈化队列(力扣)

    用队列实现栈 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push.top.pop 和 empty). 实现 MyStack 类: void push(int x ...

  3. 【队列】【225. 用队列实现栈】【简单】

    使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意:你只能使用队列的基本操作-- 也就 ...

  4. Java版数据结构之数组模拟环形队列demo

    Java版数据结构之数组模拟环形队列demo 我的代码仓库:https://github.com/zhuangbinan/datastructure 类 CircleArray package clu ...

  5. (数据结构与算法)数组模拟队列和环形队列

    文章目录 数组模拟队列 思路 代码实现 问题分析并优化 数组模拟环形队列 思路 代码实现 数组模拟队列 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则.即:先存入队列的数据,要先取 ...

  6. JAVA数据结构与算法【队列、数组模拟(环形)队列】

    队列 使用场景:排队 队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则.即:先存入队列的数据,要先取出.后存入的要后取出 示意图:(使用数组模拟队列示意图) 数组模拟队列 ...

  7. 不可上位!数据结构队列,老实排队,Java实现数组模拟队列及可复用环形队列

    文章目录 队列简介 数组模拟队列(无法复用) 数组模拟环形队列(可复用) 队列简介 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则.即先存入队列的数据,先取出,后存入的后取出. 示 ...

  8. 队列与环形队列使用数组模拟

    队列 该文是观看尚硅谷韩老师视频学习自己总结学习得,有的是来源于网络收集 队列引入 进的一端称为队尾(rear),出的一端称为队头(front).队列可以用顺序存储,也可以用链式存储. 队列介绍 队列 ...

  9. 用java模拟环形队列的实现(java)

    环形队列介绍: 队列是一个有序列表,可以用数组或者链表来实现: 遵循先入先出的原则,即:先存入队列的数据,先取出,后存入的后取出:即先进先出原则. 队列空间可以循环使用 示意图 实现思路: 这里要解释 ...

最新文章

  1. win7 能下node什么版本_Node.js 版本知多少?又该如何选择?
  2. MyBatis学习总结(9)——使用MyBatis Generator自动创建代码
  3. 计算机病毒会不会通过u盘传染,这样会感染病毒吗?
  4. 【腾讯面试题】Nginx
  5. 【案例分析】分布式系统的接口幂等性设计!
  6. HTML5崛起之时,Java桌面时代就已经终结了
  7. qq文件对方接收后一定会有提示吗_QQ要增加消息已读功能,你是否赞成?
  8. 7_less中引入其他文件 / 模块
  9. 【POI1999codevs3634】积水,关于sunshine爷的NOIP(suan)水(ge)题(pi)赛
  10. 这次是小米!5.31亿元深圳买地:将与腾讯、阿里做邻居
  11. Mybatis判断表是否存在
  12. port security violation protect retrict shutdown 之具体解释
  13. mysql更改密码_Mysql更改密码、连接mysql、mysql常用命令
  14. AutoCAD工具栏中没有工具栏选项
  15. Java、JSP电子书下载系统
  16. C语言中的清屏函数(自己编写)
  17. 基于Win32框架的OpenGL程序
  18. 【踩坑】Win11 WSL2 中 meld 无法正常使用问题修复
  19. 十款浏览器插件,让你拥有更好的浏览器体验
  20. 对比两个自定义对象是否相等

热门文章

  1. python创建长度为20的列表_如何在python中创建固定大小列表?
  2. 丢失宠物发布找寻平台
  3. 【英语:基础高阶_全场景覆盖表达】K9.口语主题陈述——饮食健康类
  4. MySQL连接错误实例
  5. RIP动态路由配置 命令| 配置题
  6. OFDM和OFDMA的主要优缺点
  7. 对展开运算符和object.assign()的理解
  8. Colossal-AI 分布式人工智能框架
  9. Mysql innodb 间隙锁
  10. Transferability vs. Discriminability: Batch Spectral Penalization for Adversarial Domain Adaptation