zcq

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