重学数据结构系列之——队列
来源:互联网 发布:程序员能自学吗 编辑:程序博客网 时间:2024/05/01 09:00
学习来源:计蒜客
队列
1.定义
你就想象一下现实中的排队,但要理想化,不能插队哦
所以队列有一个很重要的性质,就是先进先出,First In First Out(FIFO)。
2.队列的实现
#include <iostream>#include <cassert>using namespace std;class Queue{private://保存队列的具体数据的int* data;//头 尾 队列长度int head, tail, length;public://构造函数 length_input:初始队列长度Queue(int length_input){data = new int[length_input];length = length_input;//一开始队列为空,头为0,尾设置为-1//只有一个元素时,头尾都是它,head=tail=0head = 0;tail = -1;}//析构函数~Queue(){delete[] data;}//加入队列void push(int element){//队列不能满了,因为tail一般是从0到length-1,空队列就-1//若tail + 1 == length,说明队列的尾是第length个元素了,已经没位置了if (tail + 1 < length) {//尾部移动,将元素放到相应位置tail++;data[tail] = element;}}//队列元素的输出void output(){//从头遍历到尾输出for (int i = head; i <= tail; i++) {cout<<data[i]<<" ";}cout<<endl;}//返回队首元素int front(){//判断队列不为空,为空则退出//这是宏断言,条件为true,继续执行,否则退出程序assert(head <= tail);return data[head];}//删除队首元素void pop(){assert(head <= tail);//head往后移动一位就表示删除队首元素head++;}};int main(){Queue queue(100);for (int i = 1; i <= 10; i++) {queue.push(i);}queue.output();cout<<queue.front()<<endl;queue.pop();queue.output();return 0;}
3.运行结果
2.循环队列
1.引入
如果一个队列在不断的执行插入、弹出、插入、弹出……那么可以想象,当执行到head== tail==length-1 ,便不能再插入到队列中了,而此时队列其实是空的。
对于这种存储区没有用满,而队列却发生了溢出的现象,我们称之为“假上溢”,也叫“假溢出”。我们就可以使用循环队列解决了。
对于这种存储区没有用满,而队列却发生了溢出的现象,我们称之为“假上溢”,也叫“假溢出”。我们就可以使用循环队列解决了。
2.实现
#include <iostream>#include <cassert>using namespace std;class Queue{private://保存队列的具体数据的int* data;//头 尾 队列长度 队列中元素的格式int head, tail, length, count;public://构造函数 length_input:初始队列长度Queue(int length_input){data = new int[length_input];length = length_input;//一开始队列为空,头为0,尾设置为-1//只有一个元素时,头尾都是它,head=tail=0head = 0;tail = -1;count = 0;}//析构函数~Queue(){delete[] data;}//加入队列void push(int element){//元素总数小于总长才可插入if (count < length) {//这里的尾+1后要对总长取模 tail = (tail + 1) % length; data[tail] = element;//计数的也要+1 count++; }}//队列元素的输出void output(){//从头遍历到尾输出for (int i = head; i != tail+1; i = (i+1)%length) {cout<<data[i]<<" ";}cout<<endl;}//返回队首元素int front(){//判断队列不为空,为空则退出,这里用count来判断,方便多了//这是宏断言,条件为true,继续执行,否则退出程序assert(count > 0);return data[head];}//删除队首元素void pop(){assert(count > 0);//head往后移动一位就表示删除队首元素head = (head + 1) % length;count--;}};int main(){Queue queue(11);for (int i = 1; i <= 10; i++) {queue.push(i);}queue.output();//cout<<queue.front()<<endl;queue.pop();queue.pop();queue.output();queue.push(11);queue.push(12);queue.output();return 0;}
3.运行结果
0 0
- 重学数据结构系列之——队列
- 重学数据结构系列之——线性表基础
- 重学数据结构系列之——栈
- 重学数据结构系列之——哈希表
- 重学数据结构系列之——二叉树基础
- 重学数据结构系列之——二叉排序树
- 重学数据结构系列之——图的储存
- 重学数据结构系列之——八大排序算法
- 重学数据结构系列之——总结
- 重学数据结构系列之——平衡树之SB Tree(Size Blanced Tree)
- 重学数据结构系列之——森林之并查集(Disjoint set)
- 重学数据结构系列之——图论算法之FloodFill 算法
- 重学数据结构系列之——图论算法之Prim算法
- 重学数据结构系列之——图论算法之Dijkstra算法
- 重学数据结构系列之——堆及堆排序
- 重学数据结构系列之——静态查找表查找算法
- 重学数据结构系列之——图的遍历(广度优先搜索和深度优先搜索)学习来源:计蒜客
- 还债系列之数据结构——栈和队列
- 1. 动态指定编译(运行)路径
- 制作splash-安卓开发(1)
- intellij 新建javaweb项目并用tomcat 7启动
- 为阿里云ECS服务器二级域名绑定tomcat子目录,实现一个IP多个二级域名
- BestCoder Round #75 T3 King's Order HDOJ 5642
- 重学数据结构系列之——队列
- java方法调用都是值传递的理解
- 帮助文档打开后右边显示为空
- iOS基础之沙盒机制-路径获取
- 深刻理解Python中的元类(metaclass)
- 自然数幂和模板
- 数组和逗号表达式
- 字符指针的初始化以及程序分析实例
- 【案列】私人通讯录01