前言
小白跟随尚硅谷学习数据结构与算法的第二天—队列
一、队列的使用场景与介绍
队列介绍:
二、数组模拟对列
当加入数据的时候,front没有变化,rear不断增加;
当取出数据的时候,front不断增加,rear没有变化
对列本身为有序的列表,若用数组的结构来存储对列的数据,我们要设置四个参数,分别为数组前端(front),数组后端(rear),数组最大容量(maxSize),数组(arr) ,因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front及rear分别记录队列前后端的下标,front 会随着数据输出而改变,而rear则是随着数据输入而改变。
首先我们要实现的功能为addQueue,即将数据存入对列,addQueue需要有两个步骤:
- 将尾指针往后移:rear+1,当front==rear【空】
- 若尾指针 rear小于队列的最大下标 maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear ==maxSize-1【队列满】
- rear是对列的最后一个数据,front是对列的首元素前一个元素,加入数据时,raer向后移动
代码实现:
1、首先我们要编写一个ArrayQueue类
private int maxSize;//表示数组最大容量
private int front;//队列头
private int rear;//队列尾
private int[] arr; //该数组用于存放数据 模拟队列
2、创建构造器
public ArrayQueue(int sizeMax){
this.sizeMax=sizeMax;
arr=new int[maxSize];
this.front =-1; //front是指向队列头的前一个位置
this.rear=-1; //指向队列尾的最后一个数据
}
3、addQueue的代码实现
/**
* 判断对列是否满
*/
public boolean isFull(){
return rear==maxSize-1;
}
/**
* 添加数据到队列
* @param n
*/
public void addQueue(int n){
if (isFull()){ //判断是否满
System.out.println("队列满,不能加入数据");
return;
}else {
rear++;//让rear后移动
arr[rear]=n;
}
}
4、编写一个展示代码以显示队列所有数据:showQueue的代码实现
/**
*判断是否为空
*/
public boolean isEmpty(){
return rear==front;
}
//显示队列所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("对列为空,无法展示");
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]:%d\n",i,arr[i]);
}
}
5、取出数据的代码实现 getQueue:
//获取队列 出队列
public int getQueue(){
if (isEmpty()){
//通过抛出异常
throw new RuntimeException("队列空,不能取数据");
}else {
front++;
return arr[front];
}
}
6、展示头数据的代码实现 headQueue:
//显示队列的头数据,注意不是取数据
public int headQueue(){
//判断是否为空
if (isEmpty()){
System.out.println("队列空,无数据");
throw new RuntimeException("队列空,无数据");
}else
return arr[front+1];
}
7、再声明一个Main方法 测试程序
public static void main(String[] args) {
//测试
ArrayQueue arrayQueue = new ArrayQueue(3);
char key=' ';//接受用户输入
Scanner scanner=new Scanner(System.in);
boolean loop=true;
while (loop){
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):取出数据");
System.out.println("h(head):查看队列头的数据");
key=scanner.next().charAt(0);//接受一个字符
switch (key){
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数");
int value=scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res=arrayQueue.getQueue();
System.out.printf("取出的数据是%d\n",res);
} catch (Exception e) {
//TODO:handle exception;
throw new RuntimeException(e);
}
break;
case 'h':
try {
int res=arrayQueue.headQueue();
System.out.printf("队列头的数据是%d\n",res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
break;
}
}
System.out.println("程序退出");
}
通过以上代码所形成的数组存在一个问题是数组使用一次就不能再用,没有达到复用的效果,所以我们考虑使用环形对列以达到数组的复用,应用:取模%
三、数组模拟环形队列
1、分析说明
1)尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定,这个在判断队列满的时候需要注意(rear + 1)% maxSize == front [满]
2)rear == front [空]
2、思路如下
这里以一个maxSize=4的环形数组为例:
1)front 变量的含义做一个调整:front 就指向队列的第一个元素,也就是说 arr[front] 就是队列的第一个元素,front 的初始值 = 0
2)rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear 的初始值 = 0
3)当队列为满时,条件是 (rear + 1)% maxSize == front [满]
4)当队列为空时,条件是 rear == front [空]
5)当我们这样分析,队列中有效的数据的个数 (rear + maxSize - front)% maxSize
6)根据之前的数组模拟队列,可修改成一个环形队列
在源代码的基础上进行更改,代码实现:
3、在这里rear,front都进行了改动
private int maxSize;//表示数组最大容量
private int front;//指向队列头部,包含,分析出front是指向队列头的位置
private int rear;//指向队列尾后一个的位置
private int[] arr; //该数组用于存放数据 模拟队列
//创建队列的构造器
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
}
3、addCircleQueue的代码实现,这里取模的原因是因为如果不取模,front++后面就会超过maxSize-1,以maxSize=4为例,取模则只能取0,1,2,3;
//判断队列是否满
public boolean isFull() {
return (rear+1)%maxSize ==front;
}
//添加数据到队列
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列满,不能加入数据");
return;
} else {
//直接将数据加入
arr[rear] = n;
//将rear后移 这里必须取模
rear=(rear+1)%maxSize;
}
}
4、showCircleQueue的代码实现,同理,取模是为了不超过最大maxSize;
//在这里要显示front到达的有效数据
//求出当前有效数据的个数
public int size(){
return (rear+maxSize-front)%maxSize;
}
//显示队列所有数据
public void showQueue() {
if (isEmpty()) {
System.out.println("队列空,无数据,无法遍历");
return;
}
//思路:从front开始遍历 ,遍历到rear
for (int i = front; i < front+size(); i++) {
System.out.printf("arr[%d]=%d\n", (i%maxSize), arr[(i%maxSize)]);
}
}
5、headCircleQueue的代码实现
//显示队列的头数据,注意不是取数据
public int headQueue() {
//判断
if (isEmpty()) {
System.out.println("队列空,无数据");
throw new RuntimeException("队列空,无数据");
} else
return arr[front];
}
6、getCircleQueue的代码实现
//获取队列 出队列
public int getQueue() {
if (isEmpty()) {
//通过抛出异常
throw new RuntimeException("队列空,不能取数据");
} else {
//这里需要分析出front是指向队列的第一个元素
//1、先把front对应的值保存到一个临时变量
//2、将front后移,考虑取模
//3、将临时保存的变量返回
int value=arr[front];
front=(front+1)%maxSize;
return value;
}
}
留言
以上就是本章要讲的内容,本文仅仅简单复述了老师讲课的文本内容,内容有误麻烦联系。