1 package com.jyj.queue; 2 3 import java.util.Scanner; 4 5 public class CircleArrayQueueDemo { 6 public static void main(String[] args) { 7 //测试 8 System.out.println("数组模拟环形队列测试~"); 9 //注意:有一个空的位置,实际最长为3 10 CircleArrayQueue queue = new CircleArrayQueue(4); 11 char key = ' ';//接收用户输入 12 Scanner scanner = new Scanner(System.in); 13 boolean loop = true; 14 15 //输出一个菜单 16 while(loop) { 17 System.out.println("s(show):显示队列"); 18 System.out.println("a(add):添加数据到队列"); 19 System.out.println("g(get):从队列中获取元素"); 20 System.out.println("h(head):查看队列头的数据"); 21 System.out.println("e(exit):退出程序"); 22 //接收键盘输入的字符串,并且取出它的第一个字符 23 key = scanner.next().charAt(0); 24 switch(key) { 25 case 's': 26 queue.showQueue(); 27 break; 28 case 'a': 29 System.out.println("输入一个数:"); 30 int value = scanner.nextInt(); 31 queue.addQueue(value); 32 break; 33 case 'g'://抛异常了,要捕获 34 try { 35 int res = queue.getQueue(); 36 System.out.printf("取出的数据是%d ",res); 37 } catch (Exception e) { 38 //对所捕获异常的处理 39 System.out.println(e.getMessage()); 40 } 41 break; 42 case 'h'://抛异常了,要捕获 43 try { 44 int head = queue.headQueue(); 45 System.out.printf("队列头的数据是%d ",head); 46 }catch (Exception e) { 47 //对所捕获异常的处理 48 System.out.println(e.getMessage()); 49 } 50 break; 51 case 'e': 52 scanner.close(); 53 loop = false; 54 break; 55 default: 56 break; 57 } 58 } 59 System.out.println("程序退出"); 60 } 61 } 62 63 //使用数组模拟环形队列编写一个CircleArrayQueue类 64 class CircleArrayQueue { 65 private int maxSize;//数组的最大容量 66 //队列头:调整为指向队列的第一个元素,即arr[front],初始值设为0 67 private int front; 68 //队列尾:调整为指向队列的最后一个元素的后一个位置,并空出一个空间作为约定,初始值为设为0 69 private int rear; 70 private int[] arr;//用于存放数据 71 72 //创建队列的构造器 73 public CircleArrayQueue(int arrMaxSize) { 74 maxSize = arrMaxSize; 75 arr = new int[maxSize]; 76 } 77 78 //判断队列是否为空 79 public boolean isEmpty(){ 80 return front == rear; 81 } 82 83 //判断队列是否满 84 public boolean isFull(){ 85 return (rear + 1) % maxSize == front; 86 } 87 88 //添加数据到队列 89 public void addQueue(int value) { 90 //判断是否满 91 if(isFull()) { 92 System.out.println("队列满,不能加入数据"); 93 return; 94 } 95 arr[rear] = value; //rear指向最后一个元素的后一个位置,所以直接添加 96 rear = (rear + 1) % maxSize; //考虑环形,取模 97 } 98 99 //获取队列的数据,出队列 100 public int getQueue() { 101 //判空 102 if(isEmpty()) { 103 //抛出异常 104 throw new RuntimeException("队列空,不能取数据"); 105 } 106 /** 107 * front 指向队列的第一个元素 108 * 1、先把front对应的值保留到一个临时变量 109 * 2、将front后移,考虑取模 110 * 3、将临时保存的变量返回 111 */ 112 int value = arr[front]; 113 front = (front + 1) % maxSize; 114 return value; 115 } 116 //显示队列所有数据 117 public void showQueue(){ 118 //判空 119 if(isEmpty()){ 120 System.out.println("队列空,没有可显示的数据"); 121 return; 122 } 123 //注意:从front开始遍历,遍历多少个元素:元素个数怎么求? 124 for(int i = front;i < front + size();i++) { 125 System.out.printf("arr[%d] = %d ",i % maxSize,arr[i % maxSize]); 126 } 127 } 128 129 //求当前队列有效数据的个数 130 public int size(){ 131 return (rear + maxSize - front) % maxSize; 132 } 133 134 //显示队列的头数据,注意不是取出 135 public int headQueue(){ 136 //判空 137 if(isEmpty()) { 138 throw new RuntimeException("队列空,没有可取的头数据"); 139 } 140 //注意:front指向的是队列头的数据,计算front时已经做了取模的动作,front即为正确的位置。 141 return arr[front]; 142 } 143 }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/13436.html