自己做网站要多久,京东物流网站建设策划书,重庆建设施工安全管理网站,免费wordpress主题下载地址目录
前言
1.为啥要使用循环队列
2.队列的顺序表示和实现
1.定义
2.初始化
3.销毁
4.清空
5.空队列
6.队列长度
7.获取队头
8.入队
9.出队 10.遍历队列
11.完整代码 前言 本篇博客介绍栈和队列的表示和实现。
1.为啥要使用循环队列 上篇文章中我们知道了顺序队列…
目录
前言
1.为啥要使用循环队列
2.队列的顺序表示和实现
1.定义
2.初始化
3.销毁
4.清空
5.空队列
6.队列长度
7.获取队头
8.入队
9.出队 10.遍历队列
11.完整代码 前言 本篇博客介绍栈和队列的表示和实现。
1.为啥要使用循环队列 上篇文章中我们知道了顺序队列的用法但是顺序队列有个缺点就是会“假溢出”浪费大量的存储空间关于假溢出的问题个人感觉数据结构里里面的这段解释比较好就直接截图放下面了大家自行阅读吧。 图1.顺序队列假溢出的问题
2.队列的顺序表示和实现
1.定义
#define MAX_QUEUE_SIZE 100 // 循环队列的最大容量typedef int Status;
typedef int ElemType;typedef struct {ElemType *data; // 存储数据的数组int front; // 头指针指向队首元素int rear; // 尾指针指向队尾元素的下一个位置int maxSize; // 循环队列的最大容量
} CircularQueue;
2.初始化 队列初始化的时候队头和队尾指针均为0
// 初始化循环队列
Status initCircularQueue(CircularQueue *queue, int maxSize) {queue-data (ElemType *)malloc(sizeof(ElemType) * maxSize);if (!queue-data) {return 0; // 内存分配失败}queue-front queue-rear 0;queue-maxSize maxSize;return 1; // 初始化成功
}
3.销毁 释放队列存储空间
// 销毁循环队列
void destroyCircularQueue(CircularQueue *queue) {free(queue-data);
}
4.清空
// 清空循环队列
void clearCircularQueue(CircularQueue *queue) {queue-front queue-rear 0;
}
5.空队列 队头和队尾相同的时候为空队列。
// 判断循环队列是否为空
Status isEmptyCircularQueue(CircularQueue *queue) {return queue-front queue-rear;
}
6.队列长度 比较栈顶和栈顶的指针
// 获取循环队列长度
int circularQueueLength(CircularQueue *queue) {return (queue-rear - queue-front queue-maxSize) % queue-maxSize;
}
7.获取队头 获取队头元素。
// 获取循环队列的队首元素
Status getCircularQueueFront(CircularQueue *queue, ElemType *element) {if (isEmptyCircularQueue(queue)) {return 0; // 队列为空}*element queue-data[queue-front];return 1; // 成功获取队首元素
}
8.入队
// 入队
Status enCircularQueue(CircularQueue *queue, ElemType element) {if ((queue-rear 1) % queue-maxSize queue-front) {return 0; // 队列已满}queue-data[queue-rear] element;queue-rear (queue-rear 1) % queue-maxSize;return 1; // 入队成功
}
9.出队
// 出队
Status deCircularQueue(CircularQueue *queue, ElemType *element) {if (isEmptyCircularQueue(queue)) {return 0; // 队列为空}*element queue-data[queue-front];queue-front (queue-front 1) % queue-maxSize;return 1; // 出队成功
} 10.遍历队列
// 遍历循环队列
void traverseCircularQueue(CircularQueue *queue) {for (int i queue-front; i ! queue-rear; i (i 1) % queue-maxSize) {printf(%d , queue-data[i]);}printf(\n);
}
11.完整代码
int main(int argc, const char *argv[]) {CircularQueue queue;int maxSize 10; // 循环队列的最大容量initCircularQueue(queue, maxSize); // 初始化循环队列// 测试入队for (int i 1; i 5; i) {enCircularQueue(queue, i * 10);}// 输出队列元素printf(队列元素);traverseCircularQueue(queue);// 获取队首元素ElemType frontElement;if (getCircularQueueFront(queue, frontElement)) {printf(队首元素%d\n, frontElement);}// 测试出队ElemType element;for (int i 0; i 3; i) {if (deCircularQueue(queue, element)) {printf(出队元素%d\n, element);}}// 输出队列元素printf(队列元素);traverseCircularQueue(queue);// 判断队列是否为空if (isEmptyCircularQueue(queue)) {printf(队列为空\n);} else {printf(队列不为空\n);}// 获取队列长度printf(队列长度%d\n, circularQueueLength(queue));// 清空队列clearCircularQueue(queue);// 判断队列是否为空if (isEmptyCircularQueue(queue)) {printf(清空队列后队列为空\n);} else {printf(清空队列后队列不为空\n);}// 销毁队列destroyCircularQueue(queue);return 0;
}
文章转载自: http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.gywfp.cn.gov.cn.gywfp.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn