【日撸 Java 三百行】Day 18(循环队列)
目录
Day 18:循环队列
一、关于循环队列
1. 面对顺序表结构的妥协
2. header 与 tail 指针的巧妙设计
二、循环队列的方法
1. 结构定义及遍历
2. 入队
3. 出队
三、代码及测试
拓展:
小结
Day 18:循环队列
Task:
- 整除的作用.
- 想像操场跑道里放一队人, 循环的感觉就出来了.
- 为了区分空队列与满队列, 需要留一个空间. 相当于不允许首尾相连. 还是画个图吧, 否则容易进坑.
- 用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统, 从这个代码可以感受下内存的管理.
一、关于循环队列
1. 面对顺序表结构的妥协
在 Day 17 中讲过,我们之所以使用链表来实现队列,是因为使用顺序表时左侧出队的代价太大。但我们巧妙的改进队列的结构后,一切就变得简单起来。
具体的操作是:选择 逻辑上的删除 ,即利用双指针控制,令我们的首选元素的下标指向+1,从而在逻辑上屏蔽掉之前的位置的元素。
但是这个逻辑删除与添加方法有个弊端就是:假设我们不断对队列进行入队、出队、入队、出队…那么我们首尾指针位置最终会变得非常大,但是队列内的数据却还是非常少,而且之前出队后空余的位置无法被重复使用,照成极大浪费。
于是考虑用一种方法能够重用之前空余出来的无法被重复使用的空间。于是乎,我们使用了一种灵活的策略:循环队列:
这个方案是在逻辑上将我们的线性表的首位合并构造一个环,具体上来看,只要通过 i = (i + 1) % N 就可以非常方便实现这个功能设想。
一方面,这样我们不断增大的指针就可以重复利用之前释放的空间,避免浪费。另一方面,这个方案允许了尾指针位置在数组上大于头指针的情况,保证了N个空间能完全使用。
2. header 与 tail 指针的巧妙设计
我们定义头指针指向head,尾指针指向tail,其中数据有效范围为[h , t),如图:
因此,我们可以定义队空为:head == tail
那么队满如何表示呢?好像将数据填满后,队满的表示也是: head == tail
,这显然会发生冲突,所以我们做出调整,用这个语句来表示队满,即图c:(tail + 1) % N == head
二、循环队列的方法
1. 结构定义及遍历
/*** The total space. One space can never can never be used*/public static final int TOTAL_SPACE = 10;/** The data;*/int[] data;/*** The index for calculating the head. The actual head is head % TOTAL_SPACE.*/int head;/*** The index for caluculating the tail.*/int tail;/********************* * The constructor******************* */public CircleIntQueue() {data = new int[TOTAL_SPACE];head = 0;tail = 0;}// Of the first constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "";if (head == tail) {return "empty";} // Of iffor (int i = head; i < tail; i++) {resultString += data[i % TOTAL_SPACE] + ", ";} // Of for ireturn resultString;}// Of toString
2. 入队
/************************ Enqueue.* * @param paraValue The value of the new node.**********************/public void enqueue(int paraValue) {if((tail + 1)% TOTAL_SPACE == head ) {System.out.println("Queue full.");return;}// Of ifdata[tail % TOTAL_SPACE] = paraValue;tail++;}//Of enqueue
对于顺序表结构,添加元素前判断是否满是必要操作。
至于添加元素所使用到的语句。其实就是基于我们之前定义的循环顺序表递增操作 i = (i + 1) % N
;`而确定的,只不过因为我们尾指针默认在无数据的位置,故是先赋值在递增。
而且,我们的代码操作中,将取余操作从递增后立马取余推迟到了下次赋值时
3. 出队
/************************ Dequeue.* * @return The value at the head.**********************/public int dequeue() {if(head == tail) {System.out.println("No element in the queue");return -1;}//Of ifint resultValue = data[head % TOTAL_SPACE];head++;return resultValue;}//Of dequeue
顺序表的删除只需要关注是否为空即可。
三、代码及测试
package datastructure.queue;/*** Circle int queue.** @author: Changyang Hu joe03@foxmail.com* @date created: 2025-05-16*/
public class CircleIntQueue {/*** The total space. One space can never be used.*/public static final int TOTAL_SPACE = 10;/*** The data.*/int[] data;/*** The index for calculating the head. The actual head is head % TOTAL_SPACE.*/int head;/*** The index for calculating the tail.*/int tail;/********************* * The constructor******************* */public CircleIntQueue() {data = new int[TOTAL_SPACE];head = 0;tail = 0;}// Of the first constructor/*** ********************** @Title: enqueue* @Description: Enqueue.** @param paraValue The value of the new node.* @return void **********************/public void enqueue(int paraValue) {if ((tail + 1) % TOTAL_SPACE == head) {System.out.println("Queue full.");return;} // Of ifdata[tail % TOTAL_SPACE] = paraValue;tail++;}// Of enqueue/*** ********************** @Title: dequeue* @Description: Dequeue.** @return The value at the head.* @return int **********************/public int dequeue() {if (head == tail) {System.out.println("No element in the queue");return -1;} // Of ifint resultValue = data[head % TOTAL_SPACE];head++;return resultValue;}// Of dequeue/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "";if (head == tail) {return "empty";} // Of iffor (int i = head; i < tail; i++) {resultString += data[i % TOTAL_SPACE] + ", ";} // Of for ireturn resultString;}// Of toString/*** ********************** @Title: main* @Description: The entrance of the program.** @param args Not used now.* @return void **********************/public static void main(String args[]) {CircleIntQueue tempQueue = new CircleIntQueue();System.out.println("Initialized, the list is: " + tempQueue.toString());for (int i = 0; i < 5; i++) {tempQueue.enqueue(i + 1);} // Of for iSystem.out.println("Enqueue, the queue is: " + tempQueue.toString());int tempValue = tempQueue.dequeue();System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());for (int i = 0; i < 6; i++) {tempQueue.enqueue(i + 10);System.out.println("Enqueue, the queue is: " + tempQueue.toString());} // Of for ifor (int i = 0; i < 3; i++) {tempValue = tempQueue.dequeue();System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());} // Of for ifor (int i = 0; i < 6; i++) {tempQueue.enqueue(i + 100);System.out.println("Enqueue, the queue is: " + tempQueue.toString());} // Of for i}// Of main}// Of CircleIntQueue
拓展:
队列:【数据结构】队列-CSDN博客
需要说明一点,这个博客中并没有单独讲解循环队列,而是在涉及顺序队列时顺便提及了循环队列。
小结
虽然在现实生活中,循环队列的使用并不像普通链表那样队列方便和普遍,但是其在空间受限的环境的使用确实是非常方便的,只要掌握规律,很快就可以通过一般语言中的静态数组来实现。
相比于循环队列本身,我们更应该关注任务开头提及的要求:用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统。实际上,当我们循环移动队列时,已经完成了对新内存的申请和对无效内存的释放,而循环队列环的极大值,我们可以看做是操作系统的内存上线。