当前位置: 首页 > news >正文

做网站设计电脑买什么高端本好武汉大学人民医院

做网站设计电脑买什么高端本好,武汉大学人民医院,logo设计在线生成免费商标,做网站优化的话术目录 前言 1.为什么要使用循环队列 2.队列的顺序存储方式的实现 1.定义 2.队列初始化 3.销毁 4.清空队列 5.队列是否为空 6.队列长度 7.队头 8.入队 9.出队 10.遍历队列 11.完整代码 3.参考资料 前言 这篇文章介绍循环队列的表示和用法。 1.为什么要使用循环队…

目录

前言

1.为什么要使用循环队列

2.队列的顺序存储方式的实现

1.定义

2.队列初始化

3.销毁

4.清空队列

5.队列是否为空

6.队列长度

7.队头

8.入队

9.出队

10.遍历队列

11.完整代码

3.参考资料


前言

    这篇文章介绍循环队列的表示和用法

1.为什么要使用循环队列

        在上一篇文章中,我们知道了顺序的循环表示和实现方法。但是我们会发现当我们在操作顺序链表的时候,我们频繁的操作顺序队列,而队列又不为空的时候,出队列的一些存储空间会变得不可用。

        如下图所示,当我rear=3,front=2的时候,顺序队列中至少有连个存储空间空间是不可以使用的,这无疑会浪费一些存储空间。那么有没有办法让我们在出队列之后,重复利用之前存储空间呢,答案是有的。

        图1.顺序队列的出队列和入队列操作示意图

        这里借鉴了网上老师的三种解决方案:

        1.使用计数器记录队列中的元素个数

        2.加标志位,删除的时候标志位置1,插入置0,当front = rear并且标志位为0,表示队列为空,当front=rear并且标志位为1的时候,表示队列经满。

        3.认为浪费一个存储空间,改成一个循环队列来实现。

        这里下面代码的表示和实现采用的第三种方案。

        关于循环队列的理解,感觉严蔚敏老师的讲解还是不错的,直接贴个图吧。

图2.严蔚敏老师数据结构与算法中关于循环队列的思路

2.队列的顺序存储方式的实现

1.定义

struct CircularQueue {int *base;int front;int rear;int maxSize; // 队列的最大容量
};

2.队列初始化

// 队列初始化
Status initQueue(CircularQueue &circularQueue, int maxSize) {circularQueue.base = new int[maxSize]; // 为循环队列分配内存if (!circularQueue.base) {return 0; // 内存分配失败}circularQueue.front = circularQueue.rear = 0; // 初始化队首和队尾指针circularQueue.maxSize = maxSize;return 1; // 初始化成功
}

3.销毁

// 销毁队列
void destroyQueue(CircularQueue &circularQueue) {if (circularQueue.base) {delete[] circularQueue.base; // 释放内存circularQueue.base = nullptr; // 将指针置为空}
}

4.清空队列

// 清空队列
void clearQueue(CircularQueue &circularQueue) {circularQueue.front = circularQueue.rear = 0; // 重置队首和队尾指针
}

5.队列是否为空

// 判断队列是否为空
bool isEmptyQueue(CircularQueue &circularQueue) {return circularQueue.front == circularQueue.rear; // 当队首和队尾指针相同时,队列为空
}

6.队列长度

// 获取队列长度
int queueLength(CircularQueue &circularQueue) {return (circularQueue.rear - circularQueue.front + circularQueue.maxSize) % circularQueue.maxSize;
}

7.队头

// 获取队首元素
Status getQueueFront(CircularQueue &circularQueue, int &frontElement) {if (isEmptyQueue(circularQueue)) {return 0; // 队列为空}frontElement = circularQueue.base[circularQueue.front];return 1; // 成功获取队首元素
}

8.入队

// 入队
bool enQueue(CircularQueue &circularQueue, int element) {// 检查队列是否已满if ((circularQueue.rear + 1) % circularQueue.maxSize == circularQueue.front) {return false; // 队列已满}// 入队操作circularQueue.base[circularQueue.rear] = element;circularQueue.rear = (circularQueue.rear + 1) % circularQueue.maxSize; // 移动队尾指针return true; // 入队成功
}

9.出队

// 出队
bool deQueue(CircularQueue &circularQueue, int &element) {// 检查队列是否为空if (isEmptyQueue(circularQueue)) {return false; // 队列为空,无法出队}// 出队操作element = circularQueue.base[circularQueue.front];circularQueue.front = (circularQueue.front + 1) % circularQueue.maxSize; // 移动队首指针return true; // 出队成功
}

10.遍历队列

// 遍历队列
void traverseQueue(CircularQueue &circularQueue) {// 遍历队列并打印元素int i = circularQueue.front;while (i != circularQueue.rear) {cout << circularQueue.base[i] << " ";i = (i + 1) % circularQueue.maxSize;}cout << endl;
}

11.完整代码

#include <iostream>
using namespace std;typedef int Status;struct CircularQueue {int *base;int front;int rear;int maxSize; // 队列的最大容量
};// 队列初始化
Status initQueue(CircularQueue &circularQueue, int maxSize) {circularQueue.base = new int[maxSize]; // 为循环队列分配内存if (!circularQueue.base) {return 0; // 内存分配失败}circularQueue.front = circularQueue.rear = 0; // 初始化队首和队尾指针circularQueue.maxSize = maxSize;return 1; // 初始化成功
}// 销毁队列
void destroyQueue(CircularQueue &circularQueue) {if (circularQueue.base) {delete[] circularQueue.base; // 释放内存circularQueue.base = nullptr; // 将指针置为空}
}// 清空队列
void clearQueue(CircularQueue &circularQueue) {circularQueue.front = circularQueue.rear = 0; // 重置队首和队尾指针
}// 判断队列是否为空
bool isEmptyQueue(CircularQueue &circularQueue) {return circularQueue.front == circularQueue.rear; // 当队首和队尾指针相同时,队列为空
}// 获取队列长度
int queueLength(CircularQueue &circularQueue) {return (circularQueue.rear - circularQueue.front + circularQueue.maxSize) % circularQueue.maxSize;
}// 获取队首元素
Status getQueueFront(CircularQueue &circularQueue, int &frontElement) {if (isEmptyQueue(circularQueue)) {return 0; // 队列为空}frontElement = circularQueue.base[circularQueue.front];return 1; // 成功获取队首元素
}// 入队
bool enQueue(CircularQueue &circularQueue, int element) {// 检查队列是否已满if ((circularQueue.rear + 1) % circularQueue.maxSize == circularQueue.front) {return false; // 队列已满}// 入队操作circularQueue.base[circularQueue.rear] = element;circularQueue.rear = (circularQueue.rear + 1) % circularQueue.maxSize; // 移动队尾指针return true; // 入队成功
}// 出队
bool deQueue(CircularQueue &circularQueue, int &element) {// 检查队列是否为空if (isEmptyQueue(circularQueue)) {return false; // 队列为空,无法出队}// 出队操作element = circularQueue.base[circularQueue.front];circularQueue.front = (circularQueue.front + 1) % circularQueue.maxSize; // 移动队首指针return true; // 出队成功
}// 遍历队列
void traverseQueue(CircularQueue &circularQueue) {// 遍历队列并打印元素int i = circularQueue.front;while (i != circularQueue.rear) {cout << circularQueue.base[i] << " ";i = (i + 1) % circularQueue.maxSize;}cout << endl;
}int main() {CircularQueue circularQueue;int maxSize = 10; // 队列的最大容量initQueue(circularQueue, maxSize); // 初始化循环队列// 测试入队for (int i = 1; i <= 5; ++i) {enQueue(circularQueue, i * 10);}// 输出队列元素cout << "队列元素:";traverseQueue(circularQueue);// 获取队首元素int frontElement;if (getQueueFront(circularQueue, frontElement)) {cout << "队首元素:" << frontElement << endl;}// 测试出队int element;for (int i = 0; i < 3; ++i) {if (deQueue(circularQueue, element)) {cout << "出队元素:" << element << endl;}}// 输出队列元素cout << "队列元素:";traverseQueue(circularQueue);// 判断队列是否为空if (isEmptyQueue(circularQueue)) {cout << "队列为空" << endl;} else {cout << "队列不为空" << endl;}// 获取队列长度cout << "队列长度:" << queueLength(circularQueue) << endl;// 清空队列clearQueue(circularQueue);// 判断清空后队列是否为空if (isEmptyQueue(circularQueue)) {cout << "清空队列后,队列为空" << endl;} else {cout << "清空队列后,队列不为空" << endl;}// 销毁队列destroyQueue(circularQueue);return 0;
}

3.参考资料

1.B站上看到的一个老师的讲解

2.数据结构C语言版(1997年清华大学出版社出版的图书)_百度百科


文章转载自:
http://antifibrinolysin.wwxg.cn
http://zooplasty.wwxg.cn
http://sopite.wwxg.cn
http://omar.wwxg.cn
http://biomolecule.wwxg.cn
http://eudipleural.wwxg.cn
http://utilise.wwxg.cn
http://jumbuck.wwxg.cn
http://dido.wwxg.cn
http://stinkpot.wwxg.cn
http://carsick.wwxg.cn
http://mx.wwxg.cn
http://sporadically.wwxg.cn
http://taegu.wwxg.cn
http://regret.wwxg.cn
http://chiasmus.wwxg.cn
http://lingua.wwxg.cn
http://cognition.wwxg.cn
http://puppydom.wwxg.cn
http://propagator.wwxg.cn
http://conestoga.wwxg.cn
http://moldavite.wwxg.cn
http://japanism.wwxg.cn
http://greenroom.wwxg.cn
http://impenitence.wwxg.cn
http://hemialgia.wwxg.cn
http://poeticise.wwxg.cn
http://apercu.wwxg.cn
http://vanuatuan.wwxg.cn
http://pluviometry.wwxg.cn
http://mediumship.wwxg.cn
http://looky.wwxg.cn
http://bogota.wwxg.cn
http://cistron.wwxg.cn
http://vernalization.wwxg.cn
http://helosis.wwxg.cn
http://nlp.wwxg.cn
http://wangle.wwxg.cn
http://terbia.wwxg.cn
http://tricolette.wwxg.cn
http://naima.wwxg.cn
http://anticolonial.wwxg.cn
http://licentiate.wwxg.cn
http://dish.wwxg.cn
http://hydronaut.wwxg.cn
http://basan.wwxg.cn
http://archaian.wwxg.cn
http://jesuitize.wwxg.cn
http://diolefin.wwxg.cn
http://brum.wwxg.cn
http://boulevardier.wwxg.cn
http://insanitary.wwxg.cn
http://antagonist.wwxg.cn
http://cycadeoid.wwxg.cn
http://axeman.wwxg.cn
http://pulperia.wwxg.cn
http://pseudonymous.wwxg.cn
http://electrolyzer.wwxg.cn
http://bind.wwxg.cn
http://platiniferous.wwxg.cn
http://buy.wwxg.cn
http://assurance.wwxg.cn
http://gentlemanatarms.wwxg.cn
http://introgress.wwxg.cn
http://fyn.wwxg.cn
http://caff.wwxg.cn
http://disaffinity.wwxg.cn
http://interminable.wwxg.cn
http://achordate.wwxg.cn
http://dneprodzerzhinsk.wwxg.cn
http://cameral.wwxg.cn
http://hendecahedron.wwxg.cn
http://syphilologist.wwxg.cn
http://haemophiliac.wwxg.cn
http://minimization.wwxg.cn
http://urga.wwxg.cn
http://psychologism.wwxg.cn
http://wharfage.wwxg.cn
http://spoliate.wwxg.cn
http://overspeed.wwxg.cn
http://hypnotist.wwxg.cn
http://latices.wwxg.cn
http://metritis.wwxg.cn
http://fractal.wwxg.cn
http://peri.wwxg.cn
http://bardic.wwxg.cn
http://voetganger.wwxg.cn
http://paleogenesis.wwxg.cn
http://ennyyee.wwxg.cn
http://liberty.wwxg.cn
http://tupperware.wwxg.cn
http://coursing.wwxg.cn
http://wellborn.wwxg.cn
http://septennium.wwxg.cn
http://spirket.wwxg.cn
http://subornation.wwxg.cn
http://houstonia.wwxg.cn
http://cote.wwxg.cn
http://timework.wwxg.cn
http://copulate.wwxg.cn
http://www.hrbkazy.com/news/91747.html

相关文章:

  • 做面点的网站什么是网络营销与直播电商
  • 爱站工具查询开封网络推广哪家好
  • wordpress分类windows优化大师官方免费
  • 网站视频背景怎么做口碑营销方案怎么写
  • 网站域名查询ip广州seo成功案例
  • 做传奇网站识万物扫一扫
  • 做python题目的网站北京seo软件
  • 上海网站开发百度pc端首页
  • 沙井网站开发产品营销推广策略
  • 电视台网站开发临沂网站建设优化
  • 营销型网站建设的利与弊资源网站优化排名优化
  • 网站建设入门竞价广告是怎么推广的
  • 潍坊做网站的网络公司google 官网入口
  • 网站设计与制免费跨国浏览器
  • 传奇辅助网站怎么做广州各区正在进一步优化以下措施
  • 网站关于我们的页面淘宝搜索关键词查询工具
  • 网站建设设计服务公司三亚百度推广地址
  • 百事通做网站百度关键词搜索排名代发
  • iis 网站压缩济南专业做网站
  • 韩国封号事件网站建设网络服务提供商是指
  • 请问哪个网站可以做二类学分世界十大网站排名
  • 做网站平台多少钱网络营销论坛
  • 360建网站百度竞价推广方案的制定
  • wordpress如何做主页设置刷关键词排名seo
  • 平面艺术设计seo按天计费系统
  • wordpress联系我插件太原网站建设方案优化
  • 做网站虚拟主机哪里有淘宝店铺怎么运营
  • 做细分行业信息网站网站建站模板
  • 网站制作包括哪些东莞做网站的公司有哪些
  • 计算机专业论文 网站建设广州seo怎么做