1、队列基本说明

1、队列是一个有序列表,可以用数组或者链表实现
2、遵循先进先出原则,先存入数据先取出,后存入后取出

2、数组模拟队列

image.png 1、队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如上图, 其中 maxSize 是该队列的最大容量。
2、因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front及 rear分别记录队列前后端的下标,front 会随着数据输出而改变,而 rear则是随着数据输入而改变

3、数组模拟队列设计思路:

当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:
(1)将尾指针往后移:rear+1 , 前提是 判断 是否为空 取决于 front == rear 【空】
(2)若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear所指的数组元素中,否则无法存入数据。
前提判断是否 队列 容量满了 rear == maxSize - 1[队列满]

4、核心代码和测试代码:

  1. package com.kk.datastructure.queue;
  2. import java.util.Scanner;
  3. import javax.management.RuntimeErrorException;
  4. public class ArrayQueueDemo {
  5. public static void main(String[] args) {
  6. //创建一个队列
  7. ArrayQueue queue = new ArrayQueue(3);
  8. char key = ' '; //接收用户输入
  9. Scanner scanner = new Scanner(System.in);//
  10. boolean loop = true;
  11. //输出一个菜单
  12. while (loop) {
  13. System.out.println("s(show): 显示队列");
  14. System.out.println("e(exit): 退出程序");
  15. System.out.println("a(add): 添加数据到队列");
  16. System.out.println("g(get): 从队列取出数据");
  17. System.out.println("h(head): 查看队列头的数据");
  18. key = scanner.next().charAt(0);//接收一个字符
  19. switch (key) {
  20. case 's':
  21. queue.showQueue();
  22. break;
  23. case 'a':
  24. System.out.println("输出一个数");
  25. int value = scanner.nextInt();
  26. queue.addQueue(value);
  27. break;
  28. case 'g': //取出数据
  29. try {
  30. int res = queue.getQueue();
  31. System.out.printf("取出的数据是%d\n", res);
  32. } catch (Exception e) {
  33. // TODO: handle exception
  34. System.out.println(e.getMessage());
  35. }
  36. break;
  37. case 'h': //查看队列头的数据
  38. try {
  39. int res = queue.headQueue();
  40. System.out.printf("队列头的数据是%d\n", res);
  41. } catch (Exception e) {
  42. // TODO: handle exception
  43. System.out.println(e.getMessage());
  44. }
  45. break;
  46. case 'e': //退出
  47. scanner.close();
  48. loop = false;
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. System.out.println("程序退出~~");
  55. }
  56. }
  57. // 使用数组模拟一个队列:ArrayQueue类
  58. class ArrayQueue{
  59. private int maxSize;// 数组最大的容量
  60. private int front; // 队列头
  61. private int rear; // 对列尾
  62. private int[] arr; // 该数组用于模拟队列存放的数据
  63. // 创建队列构造器
  64. public ArrayQueue(int arrMaxSize) {
  65. maxSize = arrMaxSize;
  66. arr = new int[maxSize ];
  67. front = -1;// 指向队列头部,分析出目前初始化的 front 是指向队列头的前一个位置
  68. rear = -1; // 指向队列尾部,意思就是队列最后一个数据的位置
  69. }
  70. // 判断队列是否满
  71. public boolean isFull() {
  72. return rear == maxSize-1;// 队列尾 == 数组的最后一个数据
  73. }
  74. // 判断你队列是否为空
  75. public boolean isEmpty() {
  76. return front == rear;// 头尾相等,说明没数据
  77. }
  78. // 添加数据到队列
  79. public void addQueue(int n) {
  80. // 判断队列是否满
  81. if(isFull()) {
  82. System.out.println("队列已满,无法添加数据~~~~");
  83. return;
  84. }
  85. rear++; // 让 rear 后移
  86. arr[rear] = n;
  87. }
  88. // 获取队列数据(出队列)
  89. public int getQueue() {
  90. // 判断队列是否为空
  91. if(isEmpty()) {
  92. // 通过异常抛出 null
  93. throw new RuntimeException("队列为空,无法取数据~~~");
  94. }
  95. front++;// front后移
  96. int result = arr[front];
  97. arr[front] = 0;// 此时取出的值 位置存放的为0
  98. return result;
  99. }
  100. // 显示队列的所有数据
  101. public void showQueue() {
  102. // 判空
  103. if(isEmpty()) {
  104. System.out.println("队列为空,无法显示数据~~~~");
  105. return;
  106. }
  107. // 遍历
  108. for (int i = 0; i < arr.length; i++) {
  109. System.out.printf("arr[%d]=%d\n",i,arr[i]);
  110. }
  111. }
  112. // 显示头队列,而非取出
  113. public int headQueue() {
  114. // 判空
  115. if (isEmpty()) {
  116. // 通过异常抛出 null
  117. throw new RuntimeException("队列为空,无法取数据~~~");
  118. }
  119. // 显示
  120. return arr[front+1];// 这边加一的原因是一开始,初始化指向的是队列头的前一个
  121. }
  122. }

5、此队列出现的问题和优化:

1、目前设计的队列只能使用一次(因为数组只能有一次),没有达到复用效果
2、将此数组用算法改进,成一个环形队列,取模 %

6、环形队列设计思路:参考之一(实现方式有很多)

  • front 变量调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
    front 的初始值 = 0 (之前的是 front +1 才是第一个元素,初始值 -1)
  • rear 变量的调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
    rear 的初始值 = 0 (之前的是 rear 就是最后一个元素,现在是 rear -1 才是最后一个元素,初始值 -1 )
  • 当队列满时,公式是 (rear + 1) % maxSize == front (腿上面是头时,你就没法再被拉长了)
  • 对队列为空的条件, rear == front 空 (你的 头距离地面 20cm ,你的腿也是 ,那么你的肉体就不存在了)
  • 当我们这样分析, 队列中有效的数据的个数 (rear + maxSize - front) % maxSize
  • 关于第五点的公式推导,如果非环形队列有效个数是 rear-front。那么是不是就很好理解了

7、环形队列测试和代码实现

  1. package com.kk.datastructure.queue;
  2. import java.util.Scanner;
  3. public class CircleArrayQueueDemo {
  4. public static void main(String[] args) {
  5. //创建一个队列
  6. CircleArrayQueue queue = new CircleArrayQueue(4);// 此处有效数据为3
  7. char key = ' '; //接收用户输入
  8. Scanner scanner = new Scanner(System.in);//
  9. boolean loop = true;
  10. //输出一个菜单
  11. while (loop) {
  12. System.out.println("s(show): 显示队列");
  13. System.out.println("e(exit): 退出程序");
  14. System.out.println("a(add): 添加数据到队列");
  15. System.out.println("g(get): 从队列取出数据");
  16. System.out.println("h(head): 查看队列头的数据");
  17. key = scanner.next().charAt(0);//接收一个字符
  18. switch (key) {
  19. case 's':
  20. queue.showQueue();
  21. break;
  22. case 'a':
  23. System.out.println("输出一个数");
  24. int value = scanner.nextInt();
  25. queue.addQueue(value);
  26. break;
  27. case 'g': //取出数据
  28. try {
  29. int res = queue.getQueue();
  30. System.out.printf("取出的数据是%d\n", res);
  31. } catch (Exception e) {
  32. // TODO: handle exception
  33. System.out.println(e.getMessage());
  34. }
  35. break;
  36. case 'h': //查看队列头的数据
  37. try {
  38. int res = queue.headQueue();
  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 'e': //退出
  46. scanner.close();
  47. loop = false;
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. System.out.println("程序退出~~");
  54. }
  55. }
  56. //使用数组模拟一个环形队列:CArrayQueue类
  57. class CircleArrayQueue{
  58. private int maxSize;// 数组最大的容量
  59. // front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
  60. private int front;
  61. // rear 指向队列的最后一个元素的后一个位置
  62. private int rear;
  63. private int[] arr;
  64. // 创建队列构造器
  65. public CircleArrayQueue(int arrMaxSize) {
  66. maxSize = arrMaxSize;
  67. arr = new int[arrMaxSize];
  68. // front rear 默认值为0,声明的默认值也为0,既不需要重复赋值
  69. }
  70. // 判断队列是否满
  71. public boolean isFull() {
  72. return (rear + 1)% maxSize == front;
  73. }
  74. // 判断你队列是否为空
  75. public boolean isEmpty() {
  76. return front == rear;// 头尾相等,说明没数据
  77. }
  78. // 添加数据到队列
  79. public void addQueue(int n) {
  80. // 判断队列是否满
  81. if (isFull()) {
  82. System.out.println("队列已满,无法添加数据~~~~");
  83. return;
  84. }
  85. // 直接插入进来
  86. arr[rear] = n;
  87. // rear 后移, 得考虑取模(环形)
  88. rear = (rear + 1) % maxSize;
  89. }
  90. // 获取队列数据(出队列)
  91. public int getQueue() {
  92. // 判断队列是否为空
  93. if (isEmpty()) {
  94. // 通过异常抛出 null
  95. throw new RuntimeException("队列为空,无法取数据~~~");
  96. }
  97. // 分析得 front 是指向队列的第一个元素
  98. // 先 将 front 值保存到临时变量,用于返回
  99. // 然后再进行后移
  100. int value = arr[front];
  101. front = (front + 1) % maxSize;
  102. return value;
  103. }
  104. // 显示队列的所有数据
  105. public void showQueue() {
  106. // 判空
  107. if (isEmpty()) {
  108. System.out.println("队列为空,无法显示数据~~~~");
  109. return;
  110. }
  111. // 遍历(从头队列 到 都队列 + 有效数据个数)
  112. for (int i = front; i < front+size(); i++) {
  113. System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
  114. }
  115. }
  116. // 计算出有效数据个数
  117. public int size() {
  118. return (rear - front + maxSize) % maxSize;
  119. }
  120. // 显示头队列,而非取出
  121. public int headQueue() {
  122. // 判空
  123. if (isEmpty()) {
  124. // 通过异常抛出 null
  125. throw new RuntimeException("队列为空,无法取数据~~~");
  126. }
  127. // 显示
  128. return arr[front];
  129. }
  130. }