第一天:队列
下面又三种:一种是含有统计元素个数的循环队列,第二种是不含有统计个数的循环队列;至于不循环的队列在第三种中
第一种:
//有数据项计数字段的队列实现 class Queue{ private int size; private Object[] data = null; private int head; private int tail; private int count; public Queue(int s){ size = s; data = new Object[size]; head = 0; tail = -1; count = 0; } public void insert(Object ele) throws Exception{ //首先要判断元素是否已满,可否再添加 if(isFull()){ throw new Exception("空间以满,无法再添加了"); }else{ //添加元素,但添加元素前为了,让其形成循环队列,以免浪费空间,所以首先判断,tail是否到达了尾端 if(tail == size -1){ tail = -1; } data[++tail] = ele; count++; } } public boolean isFull(){ return count == size; } public Object remove() throws Exception{ //删除前判断是否为空 if(isEmpty()){ throw new Exception("元素已经空了,没有可以删除的元素了"); }else{ Object temp = data[head++]; //判断删除的head的指标是否已经到达了顶部,让其循环 if(head == size){ head = 0; } count--; return temp; } } public boolean isEmpty(){ return count == 0; } public int getSize(){ return count; } public Object peekHead(){ return data[head]; } }
第二种:
//没有数据项计数字段队列操作 class Queue2{ private int size; private Object[] data = null; private int head; private int tail; public Queue2(int s){ size = s+1; data = new Object[size]; head = 0; tail = -1; } public void insert(Object ele){ //判读是否已满 if(isFull()){ System.out.println("元素已满,不能再添加"); return; }else{ //添加元素,但添加元素前为了,让其形成循环队列,以免浪费空间,所以首先判断,tail是否到达了尾端 if(tail == size -1){ tail = -1; } data[++tail] = ele; } } public boolean isFull(){ return ( tail+2 == head || (head+size-2 == tail)); } public Object remove(){ //删除前判断是否为空 if(isEmpty()){ System.out.print("元素已经空了,没有可以删除的元素了"); return null; }else{ Object temp = data[head++]; //判断删除的head的指标是否已经到达了顶部,让其循环 if(head == size){ head = 0; } return temp; } } public boolean isEmpty(){ return ( tail+1 == head || (head+size-1 == tail)); } public int getSize(){ if(tail >= head){ return tail-head+1; }else{ return (size-head)+(tail+1); } } public Object peekHead(){ return data[head]; } }
测试代码:
public class TestDemo01 { public static void main(String args[]) throws Exception{ Queue2 q = new Queue2(3); q.insert(1); q.insert(2); q.insert(3); //q.insert(4); q.remove(); q.remove(); //q.insert(4); q.insert(4); q.insert(5); //q.insert(6); //System.out.println(q.isEmpty()); while(!q.isEmpty()){ Object temp = q.remove(); System.out.print(temp); System.out.print(" "); } } }
第三种:
class Queue{ private int size; private Object[] data; private int head; private int tail; public Queue(int initSize){ size = initSize; data = new Object[size]; head = 0; tail = 0; } public void insert(Object ele) throws Exception{ //首先判断元素是否满了,不采用循环的方式 if(tail == size){ throw new Exception("元素添加已经满了,没法再添加"); } data[tail++] = ele; } public Object remove() throws Exception{ //判读元素是否已空,//没有循环 if(tail == head){ throw new Exception("元素已经空了"); } Object temp = data[head++]; if(head == size){ throw new Exception("元素已经到底了"); } return temp; } } public class TestDemo05 { public static void main(String args[]) throws Exception{ Queue queue = new Queue(3); queue.insert(1); queue.insert(2); queue.insert(3); queue.remove(); queue.insert(4); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端