栈和队列
模拟栈
栈相对于开放的数组而言,是访问受限的
栈只允许访问一个数据项,即最后插入的数据项,移除这个数据项后才能访问倒数第二个数据项
栈的特点:先进后出
1 package com.arithmetic.stacksimulation; 2 3 /** 4 * @author 夜神 5 * @description 用Array模拟栈 用有效长度模拟动态数组操作 6 * @notice 这里是假删除 普通数组长度不可变 不能真正删除元素,要删除只有再建一个数组 7 * @really 栈相对于开放的数组而言,是访问受限的 8 * 栈只允许访问一个数据项,即最后插入的数据项,移除这个数据项后才能访问倒数第二个数据项 9 * 栈的特点:先进后出 10 */ 11 public class StackUserArray { 12 //底层是数组 13 private long[] arry; 14 // 栈顶 15 private int top; 16 17 public StackUserArray() { 18 // 初始化数组 19 arry = new long[10]; 20 // 初始化top为—1 21 top = -1; 22 } 23 24 /** 25 * 带参构造 26 * 27 * @param maxsize 28 */ 29 public StackUserArray(int maxsize) { 30 arry = new long[maxsize]; 31 top = -1; 32 } 33 34 /** 35 * 进栈 36 * 37 * @param value 38 */ 39 public void push(int value) { 40 // top先加,坐标不能为负 41 arry[++top] = value; 42 } 43 44 /** 45 * 出栈 46 * 47 * @return 48 */ 49 public long pop() { 50 return arry[top--]; 51 } 52 53 /** 54 * 查看数据 55 * 56 * @return 57 */ 58 public long peek() { 59 return arry[top]; 60 } 61 62 /** 63 * top=-1则为空 64 * 65 * @return 66 */ 67 public boolean isEmpty() { 68 return top == -1; 69 } 70 71 /** 72 * 判断栈满了没有 73 * 74 * @return 75 */ 76 public boolean isFull() { 77 return top == arry.length - 1
测试
1 public class StackTest { 2 @Test 3 public void stackT(){ 4 StackUserArray stack=new StackUserArray(4); 5 stack.push(2); 6 stack.push(4); 7 stack.push(1); 8 stack.push(6); 9 System.out.println("遍历"); 10 System.out.println("为空:"+stack.isEmpty()); 11 System.out.println("满了:"+stack.isFull()); 12 System.out.println("查看"+stack.peek()); 13 System.out.println("出栈"+stack.pop()); 14 System.out.println("查看"+stack.peek()); 15 } 16 }
队列
队列就像排队买票
只能从前面出
只能从后面进
有人出去了,余下的就会前进
通过控制队首指针,队尾指针来进行添加,移除,移动
1 package com.arithmetic.queuesimulation; 2 3 /** 4 * @author 夜神 5 * @description 模拟队列 底层数组实现 循环 6 */ 7 public class QueueUserArray { 8 // 底层数组实现 9 private long[] arr; 10 // 有效长度 11 private int effectiveLength; 12 // 队首 13 private int front; 14 // 队尾 15 private int end; 16 17 /** 18 * @description 无参构造 19 */ 20 public QueueUserArray(){ 21 arr=new long[10]; 22 effectiveLength=0; 23 front=0; 24 end=-1; 25 } 26 27 /** 28 * 带参构造 指定初始化长度 29 * @param maxsize 30 */ 31 public QueueUserArray(int maxsize){ 32 arr=new long[maxsize]; 33 effectiveLength=0; 34 front=0; 35 end=-1; 36 } 37 38 /** 39 * @description 添加队员 从队尾添加 数组的后面 40 * @param value 41 */ 42 public void insert(long value){ 43 if (end==arr.length-1){ 44 end=-1; 45 } 46 arr[++end]=value; 47 effectiveLength++; 48 } 49 50 /** 51 * @description 删除队员 队首指针向后移动一位 52 * @return 删除的值 53 */ 54 public long remove(){ 55 effectiveLength--; 56 long value=arr[front++]; 57 if (front==arr.length){ 58 front=0; 59 } 60 return value; 61 } 62 63 /** 64 * @description 查看队员 65 * @return 被查看的队员 66 */ 67 public long peek(){ 68 return arr[front]; 69 } 70 71 /** 72 * @description 查看队列是否为空 73 * @return boolean 74 */ 75 public boolean isEmpty(){ 76 return effectiveLength==0; 77 } 78 79 /** 80 * @description 队列是否满了 81 * @return boolean 82 */ 83 public boolean isFull(){ 84 return effectiveLength==arr.length; 85 }
测试
1 @Test 2 public void testQueue() { 3 QueueUserArray queue = new QueueUserArray(4); 4 queue.insert(2); 5 queue.insert(4); 6 queue.insert(6); 7 queue.insert(1); 8 System.out.println("是否满员:" + queue.isFull()); 9 System.out.println("查看队首:" + queue.peek()); 10 System.out.println("出队:" + queue.remove()); 11 System.out.println("查看队首:"+ queue.peek()); 12 System.out.println("出队后isFull? "+ queue.isFull()); 13 }
作者: deity-night
出处: https://www.cnblogs.com/deity-night/
关于作者:码农
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(***@163.com)咨询.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?