【Java】/* 链式队列 和 循环队列 - 底层实现 */

2024-08-22 23:36

本文主要是介绍【Java】/* 链式队列 和 循环队列 - 底层实现 */,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、链式队列

1. 使用双向链表实现队列,可以采用尾入,头出 也可以采用 头入、尾出 (LinkedList采用尾入、头出)

2. 下面代码实现的是尾入、头出:

package bageight;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-22* Time: 16:44*/
public class MyQueue<E> {/* 使用内部类实现链表的节点 */private class ListNode<E> {E val;ListNode<E> prev;ListNode<E> next;public ListNode(E val) {this.val = val;}}private ListNode<E> head;private ListNode<E> last;/* push尾入 */public void offer(E data) {ListNode<E> newNode = new ListNode<>(data);//1. 如果链表为nullif (this.head == null) {this.head = this.last = newNode;return;}//2. 尾插this.last.next = newNode;newNode.prev = this.last;this.last = newNode;}/* poll头出 */public E poll() {//1. 如果链表为nullif (this.head == null) {return null;}//2. 头出E del = this.head.val;if (this.head.next == null) {//链表只有一个节点this.head = this.last = null;} else {//链表有多个节点this.head = this.head.next;this.head.prev = null;}return del;}/* peek */public E peek() {//1. 如果链表为nullif (this.head == null) {return null;}//2. 正常return this.head.val;}/* size */public int size() {int count = 0;ListNode<E> cur = this.head;while (cur != null) {count++;cur = cur.next;}return count;}
}

二、循环队列

622. 设计循环队列 - 力扣(LeetCode)

【难点】:

① 到底怎么判断什么时候队列为null,什么时候队列满了

② fast++,如何不越界:fast = fast % len 或 第三种做法要写成fast = (fast + 1) % len

【等待补充】使用flag的方式如何编写代码。

2.1 使用uesdSize判断空满

class MyCircularQueue {private int[] elem;//由于不是泛型类,且依据题意放入数组中的数据都是int类型的,//因此如此定义private int head;//队头位置private int last;//待插入数据位置private int uesdSize;//队列有效数据个数public MyCircularQueue(int k) {this.elem = new int[k];}//尾插public boolean enQueue(int value) {if (this.isFull()) {return false;}this.elem[this.last] = value;this.last++;//使得last的下标一直维持在0~elem.length-1this.last = this.last % this.elem.length;this.uesdSize++;return true;}//头删public boolean deQueue() {if (this.isEmpty()) {return false;}//头删是让head的值++,而不是--🙀this.head++;this.head = this.head % this.elem.length;this.uesdSize--;return true;}//获取队头元素的值public int Front() {if (this.isEmpty()) {return -1;}return this.elem[this.head];}//获取队尾元素的值public int Rear() {if (this.isEmpty()) {return -1;}if (this.last == 0) {return this.elem[this.elem.length - 1];} return this.elem[this.last - 1];}public boolean isEmpty() {return this.uesdSize == 0;}public boolean isFull() {return this.uesdSize == this.elem.length;}
}

2.2 浪费一个数组空间来判断空满

class MyCircularQueue {private int[] elem;//由于不是泛型类,且依据题意放入数组中的数据都是int类型的,//因此如此定义private int head;//队头位置private int last;//待插入数据位置public MyCircularQueue(int k) {this.elem = new int[k + 1];}//尾插public boolean enQueue(int value) {if (this.isFull()) {return false;}this.elem[this.last] = value;this.last++;//使得last的下标一直维持在0~elem.length-1this.last = this.last % this.elem.length;return true;}//头删public boolean deQueue() {if (this.isEmpty()) {return false;}//头删是让head的值++,而不是--🙀this.head++;this.head = this.head % this.elem.length;return true;    }//获取队头元素的值public int Front() {if (this.isEmpty()) {return -1;}return this.elem[this.head];}//获取队尾元素的值public int Rear() {if (this.isEmpty()) {return -1;}if (this.last == 0) {return this.elem[this.elem.length - 1];} return this.elem[this.last - 1];}public boolean isEmpty() {return this.head == this.last;}public boolean isFull() {return (this.last + 1) % this.elem.length == this.head;//🙀,想一想特殊情况就知道为什么这么写了}
}

这篇关于【Java】/* 链式队列 和 循环队列 - 底层实现 */的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097716

相关文章

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统