2020-08-18
发布日期:2022-02-21 17:40:33 浏览次数:48 分类:技术文章

本文共 1352 字,大约阅读时间需要 4 分钟。

循环队列

是一种线性数据结构,基于队列(先进先出 FIFO),队尾被连接在队首之后形成一个循环。

与队列不同的是,队列一旦满了,即使队列前面仍然有空间,也不能够在使用,而循环队列能够使空闲的空间循环利用,存储新的值。

具体代码实现:

public class MyCircularQueue {    private int[] arr;//定义一个数组存放队列元素    private int front;//队列的头元素    private int count;//队列的当前容量    private int capacity;//队列的最大容量    public MyCircularQueue(int k) {        this.capacity = k;        this.arr = new int[k];        this.front = 0;        this.count = 0;    }   //向队列中添加元素    public boolean enQueue(int value) {        if(isFull()){            return false;        }        this.arr[(this.front +this.count)%this.capacity] = value;        this.count +=1;        return true;    }       //删除队列中的元素    public boolean deQueue() {        if(isEmpty()){            return false;        }        this.front = (this.front+1)%this.capacity;        this.count -=1;        return true;    }       //获取队列中的头元素的值    public int Front() {        if(this.count == 0){            return -1;        }        return this.arr[this.front];    }      //获取队列中尾部元素的值    public int Rear() {        if(this.count==0){            return -1;        }        rear = (this.front+this.count-1)%this.capacity;        return this.arr[rear];    }        //判断队列是否为空    public boolean isEmpty() {        return (this.count==0);    }    //判断队列是否已满    public boolean isFull() {        return (this.count == this.capacity);    }}

 

转载地址:https://blog.csdn.net/weixin_40599623/article/details/108088645 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:台式电脑机械硬盘怎么选?
下一篇:2020-10-29

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月15日 05时05分24秒