1. package com.atguigu.queue;
    2. import java.util.Scanner;
    3. /**
    4. * ClassName: <br/>
    5. * Description: <br/>
    6. * Date: 2021-02-19 13:18 <br/>
    7. * @project data_algorithm
    8. * @package com.atguigu.queue
    9. */
    10. public class CircleArrayQueueDemo {
    11. public static void main(String[] args) {
    12. //测试一把
    13. System.out.println("测试数组模拟环形队列的案例~~~");
    14. // 创建一个环形队列
    15. CircleArray queue = new CircleArray(4); //说明设置4, 其队列的有效数据最大是3
    16. char key = ' '; // 接收用户输入
    17. Scanner scanner = new Scanner(System.in);//
    18. boolean loop = true;
    19. // 输出一个菜单
    20. while (loop) {
    21. System.out.println("s(show): 显示队列");
    22. System.out.println("e(exit): 退出程序");
    23. System.out.println("a(add): 添加数据到队列");
    24. System.out.println("g(get): 从队列取出数据");
    25. System.out.println("h(head): 查看队列头的数据");
    26. key = scanner.next().charAt(0);// 接收一个字符
    27. switch (key) {
    28. case 's':
    29. queue.showQueue();
    30. break;
    31. case 'a':
    32. System.out.println("输出一个数");
    33. int value = scanner.nextInt();
    34. queue.addQueue(value);
    35. break;
    36. case 'g': // 取出数据
    37. try {
    38. int res = queue.getQueue();
    39. System.out.printf("取出的数据是%d\n", res);
    40. } catch (Exception e) {
    41. // TODO: handle exception
    42. System.out.println(e.getMessage());
    43. }
    44. break;
    45. case 'h': // 查看队列头的数据
    46. try {
    47. int res = queue.headQueue();
    48. System.out.printf("队列头的数据是%d\n", res);
    49. } catch (Exception e) {
    50. // TODO: handle exception
    51. System.out.println(e.getMessage());
    52. }
    53. break;
    54. case 'e': // 退出
    55. scanner.close();
    56. loop = false;
    57. break;
    58. default:
    59. break;
    60. }
    61. }
    62. System.out.println("程序退出~~");
    63. }
    64. }
    65. class CircleArray {
    66. private int maxSize; // 表示数组的最大容量
    67. //front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
    68. //front 的初始值 = 0
    69. private int front;
    70. //rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
    71. //rear 的初始值 = 0
    72. private int rear; // 队列尾
    73. private int[] arr; // 该数据用于存放数据, 模拟队列
    74. public CircleArray(int arrMaxSize) {
    75. maxSize = arrMaxSize;
    76. arr = new int[maxSize];
    77. }
    78. // 判断队列是否满
    79. public boolean isFull() {
    80. return (rear + 1) % maxSize == front;
    81. }
    82. // 判断队列是否为空
    83. public boolean isEmpty() {
    84. return rear == front;
    85. }
    86. // 添加数据到队列
    87. public void addQueue(int n) {
    88. // 判断队列是否满
    89. if (isFull()) {
    90. System.out.println("队列满,不能加入数据~");
    91. return;
    92. }
    93. //直接将数据加入
    94. arr[rear] = n;
    95. //将 rear 后移, 这里必须考虑取模
    96. rear = (rear + 1) % maxSize;
    97. }
    98. // 获取队列的数据, 出队列
    99. public int getQueue() {
    100. // 判断队列是否空
    101. if (isEmpty()) {
    102. // 通过抛出异常
    103. throw new RuntimeException("队列空,不能取数据");
    104. }
    105. // 这里需要分析出 front是指向队列的第一个元素
    106. // 1. 先把 front 对应的值保留到一个临时变量
    107. // 2. 将 front 后移, 考虑取模
    108. // 3. 将临时保存的变量返回
    109. int value = arr[front];
    110. front = (front + 1) % maxSize;
    111. return value;
    112. }
    113. // 显示队列的所有数据
    114. public void showQueue() {
    115. // 遍历
    116. if (isEmpty()) {
    117. System.out.println("队列空的,没有数据~~");
    118. return;
    119. }
    120. // 思路:从front开始遍历,遍历多少个元素
    121. // 动脑筋
    122. for (int i = front; i < front + size() ; i++) {
    123. System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
    124. }
    125. }
    126. // 求出当前队列有效数据的个数
    127. public int size() {
    128. // rear = 2
    129. // front = 1
    130. // maxSize = 3
    131. return (rear + maxSize - front) % maxSize;
    132. }
    133. // 显示队列的头数据, 注意不是取出数据
    134. public int headQueue() {
    135. // 判断
    136. if (isEmpty()) {
    137. throw new RuntimeException("队列空的,没有数据~~");
    138. }
    139. return arr[front];
    140. }
    141. }