Java循环队列与非循环队列的区别总结
更新时间:2021年06月22日 09:48:31 作者:小白Melody
今天给大家带来的是关于Java的相关知识总结,文章围绕着Java循环队列与非循环队列的区别展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
非循环循环队列
- 判满:(rear+1) % maxsize == front
- 判空:front == rear
- 队列元素个数:rear = (rear + maxsize - front) % maxsize
- front指针移动方式:front = (front + 1) % maxsize
- rear指针移动方式:rear= (rear+ 1) % maxsize
import java.awt.Font; import java.util.Scanner; import javax.management.RuntimeErrorException; public class CircleArrayQueueDemo { public static void main(String[] args) { //创建队列 CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while(loop) { System.out.println("s(show):显示队列"); System.out.println("e(exit):退出程序"); System.out.println("a(add):添加数据到队列"); System.out.println("g(get):从队列取出数据"); System.out.println("h(head):查看队列头的数据"); key = scanner.next().charAt(0); switch (key) { case 's': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; case 'a': System.out.println("输入一个数"); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf("取出的数据是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf("队列头的数据是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; } } System.out.println("程序退出"); } } //队列 class CircleArrayQueue{ private int maxsize; private int front; private int rear; private int[] arr; //构造器 public CircleArrayQueue(int arrmaxsize) { maxsize = arrmaxsize; front = 0; rear = 0; arr = new int[maxsize]; } //判满 public boolean isFull() { return (rear+1)%maxsize == front; } //判空 public boolean isEmpty() { return rear == front; } //入队 public void addQueue(int n) { if(isFull()) { System.out.println("队列已满,不能再添加!"); return; } //添加数据 arr[rear] = n; //rear后移 rear = (rear + 1) % maxsize; } //出队 public int getQueue() { if(isEmpty()) { throw new RuntimeException("队列为空,不可取出元素!"); } //取值 int value = arr[front]; //front后移 front = (front + 1)%maxsize; return value; } //遍历 public void showQueue() { if(isEmpty()) { System.out.println("队列为空!"); return; } for(int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]); } } //求队列有效数据的个数 public int size() { return (rear + maxsize - front) % maxsize; } //显示队头元素 public int headQueue() { if(isEmpty()) { throw new RuntimeException("队列为空,不可取出元素!"); } return arr[front]; }
结果示意图
循环队列
- 判满:(rear+1) % maxsize == front
- 判空:front == rear
- 队列元素个数:rear = (rear + maxsize - front) % maxsize
- front 指针移动方式:front = (front + 1) % maxsizer
- ear指针移动方式:rear= (rear+ 1) % maxsize
import java.awt.Font; import java.util.Scanner; import javax.management.RuntimeErrorException; public class CircleArrayQueueDemo { public static void main(String[] args) { //创建队列 CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while(loop) { System.out.println("s(show):显示队列"); System.out.println("e(exit):退出程序"); System.out.println("a(add):添加数据到队列"); System.out.println("g(get):从队列取出数据"); System.out.println("h(head):查看队列头的数据"); key = scanner.next().charAt(0); switch (key) { case 's': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; case 'a': System.out.println("输入一个数"); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf("取出的数据是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf("队列头的数据是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; } } System.out.println("程序退出"); } } //队列 class CircleArrayQueue{ private int maxsize; private int front; private int rear; private int[] arr; //构造器 public CircleArrayQueue(int arrmaxsize) { maxsize = arrmaxsize; front = 0; rear = 0; arr = new int[maxsize]; } //判满 public boolean isFull() { return (rear+1)%maxsize == front; } //判空 public boolean isEmpty() { return rear == front; } //入队 public void addQueue(int n) { if(isFull()) { System.out.println("队列已满,不能再添加!"); return; } //添加数据 arr[rear] = n; //rear后移 rear = (rear + 1) % maxsize; } //出队 public int getQueue() { if(isEmpty()) { throw new RuntimeException("队列为空,不可取出元素!"); } //取值 int value = arr[front]; //front后移 front = (front + 1)%maxsize; return value; } //遍历 public void showQueue() { if(isEmpty()) { System.out.println("队列为空!"); return; } for(int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]); } } //求队列有效数据的个数 public int size() { return (rear + maxsize - front) % maxsize; } //显示队头元素 public int headQueue() { if(isEmpty()) { throw new RuntimeException("队列为空,不可取出元素!"); } return arr[front]; } }
结果示意图
到此这篇关于Java循环队列与非循环队列的区别总结的文章就介绍到这了,更多相关Java循环队列与非循环队列内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
mybatis typeAliases 给实体类起别名的方法
这篇文章主要介绍了mybatis typeAliases 给实体类起别名,本文给大家分享两种用法,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-09-09maven仓库中心mirrors配置多个下载中心(执行最快的镜像)
这篇文章主要介绍了maven仓库中心mirrors配置多个下载中心(执行最快的镜像),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07解决gateway报netty堆外内存溢出io.netty.util.internal.OutOfDirectMemor
这篇文章主要介绍了解决gateway报netty堆外内存溢出io.netty.util.internal.OutOfDirectMemoryError,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12
最新评论