|
| 1 | +package com.cpucode.queue; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author : cpucode |
| 7 | + * @date : 2021/3/11 |
| 8 | + * @time : 22:32 |
| 9 | + * @github : https://github.com/CPU-Code |
| 10 | + * @csdn : https://blog.csdn.net/qq_44226094 |
| 11 | + */ |
| 12 | +public class CircleArrayQueue { |
| 13 | + public static void main(String[] args) { |
| 14 | + //测试一把 |
| 15 | + System.out.println("测试数组模拟环形队列的案例~~~"); |
| 16 | + |
| 17 | + // 创建一个环形队列 |
| 18 | + //说明设置4, 其队列的有效数据最大是3 |
| 19 | + CircleArray queue = new CircleArray(4); |
| 20 | + // 接收用户输入 |
| 21 | + char key = ' '; |
| 22 | + Scanner scanner = new Scanner(System.in); |
| 23 | + boolean loop = true; |
| 24 | + |
| 25 | + // 输出一个菜单 |
| 26 | + while (loop){ |
| 27 | + System.out.println("s(show): 显示队列"); |
| 28 | + System.out.println("e(exit): 退出程序"); |
| 29 | + System.out.println("a(add): 添加数据到队列"); |
| 30 | + System.out.println("g(get): 从队列取出数据"); |
| 31 | + System.out.println("h(head): 查看队列头的数据"); |
| 32 | + |
| 33 | + // 接收一个字符 |
| 34 | + key = scanner.next().charAt(0); |
| 35 | + |
| 36 | + switch (key){ |
| 37 | + case 's': |
| 38 | + queue.showQueue(); |
| 39 | + |
| 40 | + break; |
| 41 | + case 'a': |
| 42 | + System.out.println("输出一个数"); |
| 43 | + int value = scanner.nextInt(); |
| 44 | + queue.addQueue(value); |
| 45 | + |
| 46 | + break; |
| 47 | + case 'g': |
| 48 | + // 取出数据 |
| 49 | + try { |
| 50 | + int res = queue.getQueue(); |
| 51 | + System.out.printf("取出的数据是%d\n", res); |
| 52 | + } catch (Exception e) { |
| 53 | + // TODO: handle exception |
| 54 | + System.out.println(e.getMessage()); |
| 55 | + } |
| 56 | + |
| 57 | + break; |
| 58 | + case 'h': |
| 59 | + // 查看队列头的数据 |
| 60 | + try { |
| 61 | + int res = queue.headQueue(); |
| 62 | + System.out.printf("队列头的数据是%d\n", res); |
| 63 | + } catch (Exception e) { |
| 64 | + // TODO: handle exception |
| 65 | + System.out.println(e.getMessage()); |
| 66 | + } |
| 67 | + |
| 68 | + break; |
| 69 | + case 'e': |
| 70 | + // 退出 |
| 71 | + scanner.close(); |
| 72 | + loop = false; |
| 73 | + |
| 74 | + break; |
| 75 | + default: |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + System.out.println("程序退出~~"); |
| 80 | + |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class CircleArray{ |
| 85 | + /** |
| 86 | + * 表示数组的最大容量 |
| 87 | + */ |
| 88 | + private int maxSize; |
| 89 | + |
| 90 | + /** |
| 91 | + * front 变量的含义做一个调整: front 就指向队列的第一个元素, |
| 92 | + * 也就是说 arr[front] 就是队列的第一个元素front 的初始值 = 0 |
| 93 | + */ |
| 94 | + private int front; |
| 95 | + |
| 96 | + /** |
| 97 | + * rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. |
| 98 | + * 因为希望空出一个空间做为约定.rear 的初始值 = 0 |
| 99 | + * 队列尾 |
| 100 | + */ |
| 101 | + private int rear; |
| 102 | + |
| 103 | + /** |
| 104 | + * 该数据用于存放数据, 模拟队列 |
| 105 | + */ |
| 106 | + private int[] arr; |
| 107 | + |
| 108 | + public CircleArray(int arrMaxSize){ |
| 109 | + maxSize = arrMaxSize; |
| 110 | + arr = new int[maxSize]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * 判断队列是否满 |
| 115 | + */ |
| 116 | + public boolean isFull(){ |
| 117 | + return (rear + 1) % maxSize == front; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * 判断队列是否为空 |
| 122 | + */ |
| 123 | + public boolean isEmpty(){ |
| 124 | + return rear == front; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * 添加数据到队列 |
| 129 | + */ |
| 130 | + public void addQueue(int n){ |
| 131 | + //判断队列是否满 |
| 132 | + if (isFull()){ |
| 133 | + System.out.println("队列满,不能加入数据~"); |
| 134 | + |
| 135 | + return; |
| 136 | + } |
| 137 | + //直接将数据加入 |
| 138 | + arr[rear] = n; |
| 139 | + |
| 140 | + //将 rear 后移, 这里必须考虑取模 |
| 141 | + rear = (rear + 1) % maxSize; |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /** |
| 146 | + * 获取队列的数据, 出队列 |
| 147 | + */ |
| 148 | + public int getQueue(){ |
| 149 | + //判断队列是否空 |
| 150 | + if (isEmpty()){ |
| 151 | + // 通过抛出异常 |
| 152 | + throw new RuntimeException("队列空,不能取数据"); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * 这里需要分析出 front是指向队列的第一个元素 |
| 157 | + * 1. 先把 front 对应的值保留到一个临时变量 |
| 158 | + * 2. 将 front 后移, 考虑取模 |
| 159 | + * 3. 将临时保存的变量返回 |
| 160 | + */ |
| 161 | + int value = arr[front]; |
| 162 | + front = (front + 1) % maxSize; |
| 163 | + |
| 164 | + return value; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * 求出当前队列有效数据的个数 |
| 169 | + */ |
| 170 | + public int size(){ |
| 171 | + /** |
| 172 | + * rear = 2 |
| 173 | + * front = 1 |
| 174 | + * maxSize = 3 |
| 175 | + */ |
| 176 | + return (rear + maxSize - front) % maxSize; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * 显示队列的所有数据 |
| 181 | + */ |
| 182 | + public void showQueue(){ |
| 183 | + //遍历 |
| 184 | + if (isEmpty()){ |
| 185 | + System.out.println("队列空的,没有数据~~"); |
| 186 | + |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + //思路:从front开始遍历,遍历多少个元素 |
| 191 | + for (int i = front ; i < front + size(); i++) { |
| 192 | + System.out.printf("arr[%d]= %d \n", i % maxSize, arr[i % maxSize]); |
| 193 | + |
| 194 | + } |
| 195 | + } |
| 196 | + /** |
| 197 | + * 显示队列的头数据, 注意不是取出数据 |
| 198 | + */ |
| 199 | + public int headQueue(){ |
| 200 | + //判断 |
| 201 | + if (isEmpty()){ |
| 202 | + throw new RuntimeException("队列空的,没有数据~~"); |
| 203 | + } |
| 204 | + |
| 205 | + return arr[front]; |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | +} |
0 commit comments