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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月15日 05时05分24秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
1010 一元多项式求导 (25 分)
2019-04-26
1013 数素数 (20 分)
2019-04-26
1026 程序运行时间 (15 分)
2019-04-26
以太坊智能合约开发-《精通以太坊智能合约开发》学习总结实践
2019-04-26
Parabolic SAR(抛物线转向指标)
2019-04-26
java 通用内存分页(List分页)
2019-04-26
什么是WebAuthn、FIDO 是什么?
2019-04-26
Hibernate JPA-原生查询返回BigIntegers而不是long
2019-04-26
Apollo-本地开发模式(不接入配置中心进行本地开发 env=Local)
2019-04-26
拜占庭容错共识(PBFT)
2019-04-26
预言机如何读取和验证数据?
2019-04-26
git- git checkout 创建新的分支、切换分支、切换到指定tag
2019-04-26
什么是非同质化代币(NFT)\ NFT有哪些应用?
2019-04-26
VRF是什么?
2019-04-26
Qt:01---Qt Creator与SDK的下载与安装
2019-04-26
【直播回顾】获取 iOS 用户,您还可以这么做
2019-04-27
如何保证游戏长盛不衰
2019-04-27