【数据结构】LoopQueue 循环队列

2023-11-02

在这里插入图片描述

数据结构源码

接口

public interface Queue<E> {

    int getSize();

    boolean isEmpty();

    void enqueue(E e);

    E dequeue();

    E getFront();
}

实现类

public class LoopQueue<E> implements Queue<E> {

    private E[] data;

    private int front, tail;

    private int size;

    public LoopQueue(int capacity) {
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueue() {
        this(10);
    }

    public int getCapacity() {
        return data.length - 1;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return front == tail;
    }

    @Override
    public void enqueue(E e) {

        if ((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    @Override
    public E dequeue() {
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");

        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0)
            resize(getCapacity() / 2);

        return ret;
    }

    @Override
    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("Queue is empty.");

        return data[front];
    }

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity + 1];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size = %d, capacity = %d\n", size, getCapacity()));
        res.append("front [");
        for (int i = front; i != tail; i = (i + 1) % data.length) {
            res.append(data[i]);
            if ((i + 1) % data.length != tail) {
                res.append(", ");
            }
        }
        res.append("] tail");
        return res.toString();
    }

    public static void main(String[] args) {
        LoopQueue<Integer> queue = new LoopQueue<>(5);
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);

            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}

数据结构拆解

维护字段


    private E[] data;

    private int front, tail;

    private int size;

构造函数


    public LoopQueue(int capacity) {
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueue() {
        this(10);
    }


    @Override
    public void enqueue(E e) {

        if ((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    @Override
    public E dequeue() {
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");

        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0)
            resize(getCapacity() / 2);

        return ret;
    }


    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity + 1];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    public int getCapacity() {
        return data.length - 1;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return front == tail;
    }

    @Override
    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("Queue is empty.");

        return data[front];
    }

测试用例

    public static void main(String[] args) {
        LoopQueue<Integer> queue = new LoopQueue<>(5);
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);

            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【数据结构】LoopQueue 循环队列 的相关文章

随机推荐

  • 寻找最大整数——从键盘输入四个整数,找出其中的最大值并将其输出

    问题描述 从键盘输入四个整数 找出其中的最大值并将其输出 输入说明 输入4个整数 用空格分隔 输出说明 输出值最大的一个整数 输入样例 25 99 46 0 输出样例 99 include
  • 编程实现朴素贝叶斯分类算法

    from sklearn datasets import load iris iris load iris from sklearn naive bayes import GaussianNB 高斯分布型 gnb GaussianNB 构造
  • Chapter4 Duality theory对偶理论--Introduction to linear optimization

    组会讲稿 传到这里分享下
  • 未名湖边的烦恼 蓝桥杯

    问题描述 每年冬天 北大未名湖上都是滑冰的好地方 北大体育组准备了许多冰鞋 可是人太多了 每天下午收工后 常常一双冰鞋都不剩 每天早上 租鞋窗口都会排起长龙 假设有还鞋的m个 有需要租鞋的n个 现在的问题是 这些人有多少种排法 可以避免出现
  • 前端配置跨域代理

    跨域时对于前后端开发中一个非常常见的问题 当我们客户端向我们的服务器请求接口数据的时候 我们可以请求到服务器当中的数据 但是我们把数据返回我们的客户端的时候就会产生跨域问题 所以 跨域是针对我们浏览器设置一个安全策略 就是当我们的协议 域名
  • Handler processing failed; nested exception is java.lang.NoClassDefFoundError

    在使用阿里云发送短信接口时出现此错误 原因是springmvcjar包和阿里云jar包出现冲突 建议使用下面两个版本
  • 【工具】VirtualBox虚拟机安装Windows操作系统

    前面的文章中介绍了VirtualBox虚拟机的安装 VirtualBox虚拟机中如何安装操作系统 是本文的重点 下面将进行详细介绍 使用VirtualBox虚拟机安装Windows操作系统有很多好处 主要包括以下几点 节省资源 通过虚拟化技
  • Spring Boot将声明日志步骤抽离出来做一个复用类

    上文Spring Boot日志基础使用 设置日志级别中我们写了个比较基本的日志操作 但也随之产生了一个问题 我们这行代码 能不能不写 具体说 我们不希望每个需要日志的类都声明一个在这 看着太不美观了 我们最简单方法当然是继承 我们找个目录创
  • 论python自动化测试(3)- 自动化框架及工具

    python自动化测试 3 自动化框架及工具 1 概述 手续的关于测试的方法论 都是建立在之前的文章里面提到的观点 功能测试不建议做自动化 接口测试性价比最高 接口测试可以做自动化 后面所谈到的 测试自动化 也将围绕着 接口自动化 来介绍
  • Linux Common Comment in Practices

    Linux中的命令的确是非常多 但是我们只需要掌握我们最常用的命令就可以了 当然你也可以在使用时去找一下man 他会帮你解决不少的问题 然而每个人玩Linux的目的都不同 所以他们常用的命令也就差异非常大 因为不想在使用是总是东查西找 所以
  • 网络安全等级保护合规一览

    公众号关注 WeiyiGeek 将我设为 特别关注 每天带你玩转网络安全运维 应用开发 物联网IOT学习 0x00 前言 0x01 等保2 0基本要求 0x02 等保定级 1 定级流程 2 定级比较 3 定级通用要求 0x03 合规流程 0
  • 自动化平台搭建之定制log系统

    log系统概述 我们搭建的自动化平台 无论是Web和Android 都少不了一个重要的模块 那就是log输出模块 该模块记录了整个自动化平台运行期间的日志记录 完成自动化测试后 我们可以通过日志追踪和分析fail项 根据自动化平台log输出
  • Intellj IDEA基础设置

    基础配置 view toolbar 配置jdk configure project defaults project structure new jdk 路径 添加插件 configure plugins 配置jvm内存 configure
  • Bootstrap的CSS类积累学习

    要看哪个的介绍 搜索关键词就行了 001 container 这是Bootstrap中定义的一个CSS类 它用于创建一个具有固定宽度的容器 比如 container类将 div 元素包装成一个固定宽度的容器 详情见 https blog c
  • STL vector的N种构造方式

    1 使用默认无参的构造函数进行构造 vector
  • 设计一算法,将已建立的单链表进行逆置

    单链表逆序有很多种方法 可是好多种方法都是逆序后就不能再使用之前定义的函数了 因为你的头结点变动了 不再是之前所定义的first或是head了 所以之前的方法都要重写 后来我终于想到了种很好的方法了 为了不重开空间 我们可以就在原来的那个单
  • leetcode:37. 解数独

    题目链接 37 解数独 文章目录 题目描述 思路 代码 题目描述 编写一个程序 通过填充空格来解决数独问题 数独的解法需 遵循如下规则 数字 1 9 在每一行只能出现一次 数字 1 9 在每一列只能出现一次 数字 1 9 在每一个以粗实线分
  • git lfs搭建 —— ubuntu20.04

    一直使用git lab 临时需要放一些pdf文档但有不需要git来版本管理 个人感觉比较占用资源 百度了一通 决定用git lfs 同时发现git lab有内置lfs使用说明 结合网上查得 总结如下 也是个人操作留档 本人使用vscode
  • 学生的姓名 ,年龄,性别,班级及爱好IDEA代码

    学生的姓名 年龄 性别 班级及爱好AIDE代码 package zy 学生类 class Person 属性 姓名 年龄 性别 班级 爱好 String name 姓名 int age 年龄 String sex 性别 int classN
  • 【数据结构】LoopQueue 循环队列

    数据结构源码 接口 public interface Queue