当前位置:   article > 正文

java----循环队列_循环队列java

循环队列java

一、什么是循环队列

循环队列(Circular Queue) 是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个,成为循环队列。循环队列可以更简单防止伪溢出的发生,但队列大小是固定的。

二、循环队列的简单实现

环形队列通常使用数组实现

1、简单图示

在这里插入图片描述

2.数组下标循环的小技巧:

  1. 队尾元素的下标rear的移动: rear = ( rear + 1) % elem.length;
    头元素的下标:front = (front + 1) % elem.length
  2. 如何区分空与满
    a、判空:front=rear
    b、判满:front=(rear+1)%MaxSize(MaxSize 代表数组的长度)
    在这里插入图片描述
    3.代码实现
要求:
*    设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)
*    原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
*
*    循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个
*    队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能
*    使用这些空间去存储新的值。
*
*    你的实现应该支持如下操作:
*    MyCircularQueue(k): 构造器,设置队列长度为 k 。
*    Front: 从队首获取元素。如果队列为空,返回 -1*    Rear: 获取队尾元素。如果队列为空,返回 -1*    enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
*    deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
*    isEmpty(): 检查循环队列是否为空。
*    isFull(): 检查循环队列是否已满。
*/
class CircularQueue {
    private int[] elem;//数组
    private int front;//头元素的下标
    private int rear;//尾元素的下标

    public CircularQueue(int k) {
        //这里为什么是k+1,题目要求是放k个元素
        this.elem = new int[k + 1];
        this.front = 0;
        this.rear = 0;
    }

    //检查循环队列是否已满
    public boolean isFull() {
        if((this.rear + 1) % this.elem.length == this.front){
            return true;
        }
        return false;
    }

    //向循环队列插入一个元素。如果成功插入则返回真。
    public boolean enQueue(int value) {
        if(isFull()) return false;
        this.elem [this.rear] = value;
        this.rear = ( this.rear + 1) % this.elem.length;
        return true;
    }

    // 从循环队列中删除一个元素。如果成功删除则返回真。
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        this.front = (this.front + 1) % this.elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return this.elem[this.front];

    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        //当rear == 0时,下标是this.elem.length-1
        //当rear != 0时, 下标是this.rear-1
        int index = (this.rear == 0)? this.elem.length-1:this.rear -1;
        return this.elem[index];
    }

    public boolean isEmpty() {
        //当front和rear相遇时,就是空的队列
        if(this.front == this.rear){
            return true;
        }
        return false;
    }
}
public class CircularQueueTest {
    public static void main(String[] args) {
        CircularQueue  queue = new CircularQueue(3);
        System.out.println(queue.enQueue(1));//true
        System.out.println(queue.enQueue(2));//true
        System.out.println(queue.Rear());//2
        System.out.println(queue.isEmpty());//false
        System.out.println(queue.isFull());//false
        System.out.println(queue.Front());//1
        System.out.println(queue.deQueue());//true
        System.out.println(queue.Rear());//2
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

总结

以上内容就是个人对循环队列的学习和简单实现,后续有深刻的学习之后会继续改进。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/884829
推荐阅读
相关标签
  

闽ICP备14008679号