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

【日撸 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博客

        需要说明一点,这个博客中并没有单独讲解循环队列,而是在涉及顺序队列时顺便提及了循环队列。


小结 

        虽然在现实生活中,循环队列的使用并不像普通链表那样队列方便和普遍,但是其在空间受限的环境的使用确实是非常方便的,只要掌握规律,很快就可以通过一般语言中的静态数组来实现。

        相比于循环队列本身,我们更应该关注任务开头提及的要求:用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统。实际上,当我们循环移动队列时,已经完成了对新内存的申请和对无效内存的释放,而循环队列环的极大值,我们可以看做是操作系统的内存上线。

相关文章:

  • 101. 对称二叉树
  • MGX:多智能体管理开发流程
  • 时钟产生的公共模块示例
  • C++动态内存分配
  • 【AI面试秘籍】| 第11期:大模型“复读机“难题的破局之道
  • Vue百日学习计划Day9-15天详细计划-Gemini版
  • STM32 ADC+DMA+TIM触发采样实战:避坑指南与源码解析
  • 如何有效的开展接口自动化测试?
  • 面试题:详细分析Arraylist 与 LinkedList 的异同
  • 【Spring AI】本地大模型接入MCP实现联网搜索
  • 综合项目:博客
  • Python之三大基本库——Matplotlib
  • 对称二叉树的判定:双端队列的精妙应用
  • 源码:处理文件格式和字符集的相关代码(3-3)
  • Spring WebFlux与Quarkus实战:云原生微服务开发的两大主流框架深度解析
  • 一分钟了解机器学习
  • Linux系统启动相关:vmlinux、vmlinuz、zImage,和initrd 、 initramfs,以及SystemV 和 SystemD
  • 割点与其例题
  • 消防应急处置管理的全流程概述
  • NLP双雄争霸:GPT与BERT的生成-理解博弈——从技术分野到产业融合的深度解码
  • 广州市政府网站建设与管理规范/收录入口在线提交
  • 网站站点建设/免费一键搭建网站
  • 海外独立站平台/长沙seo优化推广公司
  • 广告设计作品图片/百度搜索关键词排名优化技术
  • 郑州付费系统网站开发建设/自媒体平台app
  • 公司网站可以自己做么/网络推广企业