Java数据结构与算法之循环队列的实现
作者:我是小白呀 发布时间:2023-11-02 11:51:29
概述
从今天开始, 小白我将带大家开启 Jave 数据结构 & 算法的新篇章.
循环队列
循环队列 (Circular Queue) 是一种特殊的队列. 循环队列解决了队列出队时需要将所有数据前移一位 (复杂度为 O(n)) 的问题.
循环队列的底层依然是数组, 不过增加了指向头和尾的指针.
循环队列实现
判断队列是否为空: 当头指针 Q.front == 尾指针 Q.rear, 队列为空
判断队列是否为满: 当头指针 Q.front == 尾指针 Q.rear + 1) % Maxsize, 队列为满
改变队列大小
// 改变队列大小
private void resize(int capacity) {
// 定义新数组
E[] newData = (E[]) new Object[capacity + 1];
// 转移数组元素
for (int i = 0; i < size; i++) {
newData[i] = data[(i +front) % data.length];
}
// 更新
data = newData;
front = 0;
rear = size;
}
enqueue 方法
// 入队
public void enqueue(E e) {
// 判断是否为满
if((rear + 1) % data.length == front) {
resize(getCapacity() * 2);
}
// 队伍加入
data[rear] = e;
rear = (rear + 1) % data.length;
size++;
}
dequeue 方法
// 出队
public E dequeue() {
// 判断是否为空
if(isEmpty()) {
throw new RuntimeException("队列为空");
}
// 取出第一个元素
E element = data[front];
data[front] = null;
// 移动头指针
front = (front + 1) % data.length;
// 数组大小-1
size--;
// 判断是否需要缩小
if(size==getCapacity() / 4 && getCapacity() / 2 != 0) {
resize(getCapacity() / 2);
}
return element;
}
main
// 主函数
public static void main(String[] args) {
// 创建循环队列
CircularQueue<Integer> queue = new CircularQueue<>(5);
// 入队
for (int i = 0; i < 8; i++) {
queue.enqueue(i);
System.out.println(queue);
}
// 出队
for (int i = 0; i < 8; i++) {
queue.dequeue();
System.out.println(queue);
}
}
输出结果:
CircularQueue{data=[0, null, null, null, null, null], front=0, rear=1, size=1}
CircularQueue{data=[0, 1, null, null, null, null], front=0, rear=2, size=2}
CircularQueue{data=[0, 1, 2, null, null, null], front=0, rear=3, size=3}
CircularQueue{data=[0, 1, 2, 3, null, null], front=0, rear=4, size=4}
CircularQueue{data=[0, 1, 2, 3, 4, null], front=0, rear=5, size=5}
CircularQueue{data=[0, 1, 2, 3, 4, 5, null, null, null, null, null], front=0, rear=6, size=6}
CircularQueue{data=[0, 1, 2, 3, 4, 5, 6, null, null, null, null], front=0, rear=7, size=7}
CircularQueue{data=[0, 1, 2, 3, 4, 5, 6, 7, null, null, null], front=0, rear=8, size=8}
CircularQueue{data=[null, 1, 2, 3, 4, 5, 6, 7, null, null, null], front=1, rear=8, size=7}
CircularQueue{data=[null, null, 2, 3, 4, 5, 6, 7, null, null, null], front=2, rear=8, size=6}
CircularQueue{data=[null, null, null, 3, 4, 5, 6, 7, null, null, null], front=3, rear=8, size=5}
CircularQueue{data=[null, null, null, null, 4, 5, 6, 7, null, null, null], front=4, rear=8, size=4}
CircularQueue{data=[null, null, null, null, null, 5, 6, 7, null, null, null], front=5, rear=8, size=3}
CircularQueue{data=[6, 7, null, null, null, null], front=0, rear=2, size=2}
CircularQueue{data=[7, null, null], front=0, rear=1, size=1}
CircularQueue{data=[null, null], front=0, rear=0, size=0}
完整代码
import java.util.ArrayList;
import java.util.Arrays;
public class CircularQueue<E> {
private E[] data;
private int front, rear;
private int size;
// 无参构造
public CircularQueue() {
this(10);
}
// 有参构造
public CircularQueue(int capacity) {
data = (E[]) new Object[capacity + 1];
front = rear = size = 0;
}
// 入队
public void enqueue(E e) {
// 判断是否为满
if((rear + 1) % data.length == front) {
resize(getCapacity() * 2);
}
// 队伍加入
data[rear] = e;
rear = (rear + 1) % data.length;
size++;
}
// 出队
public E dequeue() {
// 判断是否为空
if(isEmpty()) {
throw new RuntimeException("队列为空");
}
// 取出第一个元素
E element = data[front];
data[front] = null;
// 移动头指针
front = (front + 1) % data.length;
// 数组大小-1
size--;
// 判断是否需要缩小
if(size==getCapacity() / 4 && getCapacity() / 2 != 0) {
resize(getCapacity() / 2);
}
return element;
}
// 获取队列大小
public int getSize() {
return size;
}
// 获取队列容量
public int getCapacity() {
return data.length - 1;
}
// 队列是否为空
public boolean isEmpty() {
return front == rear;
}
// 改变队列大小
private void resize(int capacity) {
// 创建新数组
E[] newData = (E[]) new Object[capacity + 1];
// 转移数组元素
for (int i = 0; i < size; i++) {
newData[i] = data[(i +front) % data.length];
}
// 更新
data = newData;
front = 0;
rear = size;
}
@Override
public String toString() {
return "CircularQueue{" +
"data=" + Arrays.toString(data) +
", front=" + front +
", rear=" + rear +
", size=" + size +
'}';
}
// 主函数
public static void main(String[] args) {
// 创建循环队列
CircularQueue<Integer> queue = new CircularQueue<>(5);
// 入队
for (int i = 0; i < 8; i++) {
queue.enqueue(i);
System.out.println(queue);
}
// 出队
for (int i = 0; i < 8; i++) {
queue.dequeue();
System.out.println(queue);
}
}
}
来源:https://blog.csdn.net/weixin_46274168/article/details/121882681


猜你喜欢
- 问题说明:IDEA编译的时候乱码,Build Output提示信息乱码�����。解决方案一:将Help—>Edit Cusuom V
- springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些s
- class ftp{ private string host = null; &n
- 一、构造方法概述构造方法是一种特殊的方法作用:创建对象Student stu = new Student();格式:pucli class
- StringRedisTemplate与RedisTemplate区别点两者的关系是StringRedisTemplate继承RedisTe
- 一、概述Overview - LINQ to XML | Microsoft 官方文档LINQ to XMLLINQ to XML 是一种启
- 1、比较器①比较器的引入a.首先,当我们单一地比较某一种数据类型的数组时,可以直接用Arrays.sort()进行实现b.而当我们同时含有多
- 本文实例为大家分享了Android点击缩略图放大效果的具体代码,供大家参考,具体内容如下import android.animation.A
- 前言在上一章节Spring和Mybatis整合的原理详解中有写到Spring和MyBatis整合时用到的Bean扫描是Spring本身提供的
- 方案一.使用国内的镜像阿里仓库等首先通过maven的路径找到setting.xml的文件然后在其中修改mirror和profile保存一下就
- progressDialog, 它有两个方法dialog.cancel() 和 dialog.dimiss()1. public void
- 软件工程由于需要不断迭代开发,因此要对源代码进行版本管理。Android源代码工程(AOSP)也不例外,它采用Git来进行版本管理。AOSP
- 这几天做项目,有些地方的图片需要用到圆形图片,所以百度了一下,在github上找到一个开源项目,处理很简单,效果如下:使用起来特别简单,一共
- 近期遇到了DateTime到底是值类型还是引用类型的疑惑,顺势较深入地了解一下DateTime相关的内容结论:DateTime是值类型,因为
- 线程池示例在分析线程池之前,先看一个简单的线程池示例。import java.util.concurrent.Executors;impor
- 最近一直都在学习Java,发现目前Java招聘中,mybatis出现的频率挺高的,可能是目前Java开发中使用比较多的数据库ORM框架。于是
- 本文实例为大家分享了Android文件下载功能的具体代码,供大家参考,具体内容如下1.普通单线程下载文件:直接使用URLConnection
- 不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录action,就可以实现登录到其他系统。ssh框架项
- RollViewPager是一个自动轮播的Viewpager,支持无限循环。 触摸时会暂停播放,直到结束触摸一个延迟周期以后继续播放。 看起
- 一,Thread 的几个常见属性Thread 类是 JVM 用来管理线程的一个类,换句话说,每个线程都有一个唯一的 Thread 对象与之关