传统数组实现的队列有缺陷,当多次入队出队后,队头指针会后移,当队尾指针达到数组末尾时,会提示队列已满,导致数组前部分空间被浪费。如果当队尾和队头指针到达数组末尾时能从数组[0]继续添加数据,可以提升数组空间利用率。

  循环数组也包含队头,队尾指针。每次入队/出队 都利用+1取模的方法(加一后如果大于数组长度,则会从0开始计算),让队头、队尾指针不会超出数组长度,实现循环的效果。

  (head+1)%arr.length

代码如下(源自LeetCode):

 package ABAB;

 class MyCircularQueue {

     private int[] data;     //储存元素的数组
private int head; //队头指针
private int tail; //队尾指针
private int size; //队列容量 /**
* 初始化队列,队头、队尾置位-1
*/
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
data = new int[k];
head = -1;
tail = -1;
size = k;
} /**
* 入队,队尾+1,并且保存数据。如果此时为空队列,队头也要置位0,因为只有一个元素时,他既是队头,又是队尾。
*/
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if (isFull() == true) {
return false;
}
if (isEmpty() == true) {
head = 0;
}
tail = (tail + 1) % size;
data[tail] = value;
return true;
} /**
* 出队,增加了队列是否为空的判断,如果队头==队尾(队列为空),则将这两个指针置为-1,若不为空则队头+1
*/
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if (isEmpty() == true) {
return false;
}
if (head == tail) {
head = -1;
tail = -1;
return true;
}
head = (head + 1) % size;
return true;
} /** Get the front item from the queue. */
public int Front() {
if (isEmpty() == true) {
return -1;
}
return data[head];
} /** Get the last item from the queue. */
public int Rear() {
if (isEmpty() == true) {
return -1;
}
return data[tail];
} /**
* 判断是否为空使用的是判断head是否为-1。在已经有元素入队以后,head不会等于-1。在出队操作时,如果队列已为空(head==tail),会将head置为-1,此时调用isEmpty为true.
*/
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return head == -1;
} /**
* 判断是否为满,如果((tail + 1) % size) == head,说明队尾已经循环了一圈,到达队头的前面了,此时队列为满。
*/
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return ((tail + 1) % size) == head;
}
}

Java 循环队列的更多相关文章

  1. Java 循环队列的实现

    队列概念 队列(Queue)是限定只能在一端插入.另一端删除的线性表.允许删除的一端叫做队头(front),允许插入的一端叫做队尾(rear),没有元素的队列称为“空队列”. 队列具有先进先出(FIF ...

  2. java循环队列实现代码

    public class Queue { //队首指针 private int front; //队尾指针 private int rear; //数组 private int[] arr; //数组 ...

  3. 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列

    一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其 ...

  4. Java实现一个简单的循环队列

    在某些时候,我们不能被要求像数组一样可以使用索引随机访问,而是需要被限制顺序处理业务,今天介绍一种先进先出(FIFO)的线性数据结构:队列, 当然,还有后进先出(LIFO)的处理方式,即为栈(后续有时 ...

  5. Java数据类型Stack栈、Queue队列、数组队列和循环队列的比较

    判断括号是否匹配:调用java本身 import java.util.Stack; public class Solution { public boolean isValid(String s){ ...

  6. Java数组实现循环队列的两种方法

    用java实现循环队列的方法: 1.添加一个属性size用来记录眼下的元素个数. 目的是当head=rear的时候.通过size=0还是size=数组长度.来区分队列为空,或者队列已满. 2.数组中仅 ...

  7. 【Java】 大话数据结构(7) 循环队列和链队列

    本文根据<大话数据结构>一书,实现了Java版的循环队列.链队列. 队列:只允许在一端进行插入操作,而在另一端进行删除操作的线性表. 1.循环队列 队列的顺序储存结构:用数组存储队列,引入 ...

  8. java数据结构——队列、循环队列(Queue)

    每天进步一点点,坚持就是成功. 1.队列 /** * 人无完人,如有bug,还请斧正 * 继续学习Java数据结构————队列(列队) * 队列和栈一样,都是使用数组,但是队列多了一个队头,队头访问数 ...

  9. Java实现循环队列

    一.分析 队列是一种先进先出的线性表,它只允许在表的一端进行插入,而在另一端删除元素.允许插入的一端称为队尾,允许删除的一端称为队头. 循环队列是一种以顺序存储结构表示的队列,为了解决“假溢出”问题而 ...

随机推荐

  1. 【图数据库】史上超全面的Neo4j使用指南

    转自:https://cloud.tencent.com/developer/article/1336299 在这篇文章中: 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章 ...

  2. element ui 批量删除

    <el-table :data="tableData" stripe border style="width: 100%" @selection-chan ...

  3. js求1到任意数之间的所有质数

    何为质数: 只能被1 和 自身 整除的数; 方法: 利用js中求模, 看是否有余数. ---> 3%2 = 1; 5%2 = 3......... 代码如下: function test (n) ...

  4. H3C查看、删除已经保存配置文件--用户图示(console)以上

    <H3C>display saved-configuration    //显示已经保存的内容 <H3C>reset saved-configuration      //删除 ...

  5. 关于git命令

    1在自己电脑上生成一个密钥然后给老大,老大在github上设置后,你这台电脑才可以访问他的项目ssh-keygen -t rsa -C <邮件名称> ------->获取秘钥 cat ...

  6. eslint 错误提示

    “Missing semicolon.” : “缺少分号.”, “Use the function form of \”use strict\”.” : “使用标准化定义function.”, “Un ...

  7. Kakuro Extension HDU - 3338 (Dinic)

    Kakuro puzzle is played on a grid of "black" and "white" cells. Apart from the t ...

  8. 2018-8-10-win10-uwp-按下等待按钮

    title author date CreateTime categories win10 uwp 按下等待按钮 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 ...

  9. CAS5.3 单点登录/登出/springboot/springmvc

    环境: jdk:1.8 cas server:5.3.14 + tomcat 8.5 cas client:3.5.1 客户端1:springmvc 传统web项目(使用web.xml) 客户端2:s ...

  10. Team Foundation Server 2015使用教程【6】:新增权限为读取器的团队