• 数据结构(java)--队列1


    一、我们还是依旧引入一个小例子(银行排队):

    需要取号排队,服务完下一个

    二、队列介绍

    1)队列是一个有序列表,可以用数组或是链表来实现

    2)遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出

    3)示意图:(使用数组模拟队列示意图)如图插入数据查询数据

    三、数组模拟队列思路

    队列本身是有序列表,若使用数组的结构来储存队列的数据,则队列数组的声明如下图,其中maxSize是该队列的最大容量

    因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front及rear分别记录队列前后端的下标,front会随着数据的输出而改变,而rear则是随着数据的输入而改变,如图所示:

    当我们将数据存入队列时称谓“addQueue”,addQueue的处理需要有两个步骤:思路分析

    1)将尾指针往后移:rear+1.当front==rear【空】

    2)若尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数据元素中,否则无法存入数据,rear == maxSize -1【队列满】

    四、java代码实现

    1. package com.atguigu.queue;
    2. import java.util.Scanner;
    3. public class ArrayQueueDemo {
    4. public static void main(String[] args) {
    5. // 测试一把
    6. // 创建一个队列
    7. ArrayQueue arrayQueue = 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. arrayQueue.showQueue();
    22. break; // Added break to exit the switch after each case
    23. case 'a':
    24. System.out.println("输入一个数");
    25. int value = scanner.nextInt();
    26. arrayQueue.addQueue(value); // Changed 'queue' to 'arrayQueue'
    27. break;
    28. case 'g': // 取出数据
    29. try {
    30. int res = arrayQueue.getQueue(); // Changed 'queue' to 'arrayQueue'
    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 = arrayQueue.headQueue(); // Changed 'queue' to 'arrayQueue'
    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 rear == front;
    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. // 通过抛出异常
    93. throw new RuntimeException("队列空,不能获取数据");
    94. }
    95. front++;// front后移
    96. return arr[front];
    97. }
    98. // 显示队列的所有数据
    99. public void showQueue() {
    100. // 遍历
    101. if (isEmpty()) {
    102. System.out.println("队列空的,没有数据~~");
    103. return;
    104. }
    105. for (int i = 0; i <= rear; i++) { // Modified loop condition
    106. System.out.printf("arr[%d]=%d\n", i, arr[i]);
    107. }
    108. }
    109. // 显示队列的头数据,注意不是取出数据
    110. public int headQueue() {
    111. // 判断
    112. if (isEmpty()) {
    113. System.out.println("队列空的,没有数据~~");
    114. // Return a default value when queue is empty
    115. return -1;
    116. }
    117. return arr[front + 1];
    118. }
    119. }

    五、在运行中代码有明显的不足之处,插入数据,再取出数据后,无法插入显示队列满,而这也就引出了我们对于数组需要进行复用以及环形的队列问题,敬请期待队列2

     

  • 相关阅读:
    JavaScript高级知识-浏览器的解析以及JS运行原理
    力扣刷题之2970.统计移除递增子数组的数目I
    appium实现自动化测试原理
    (附源码)ssm教师工作量核算统计系统 毕业设计 162307
    java中的DTO
    【踩坑日记 · 前端】为 Excalidraw 添加中文手写字体
    java计算机毕业设计基于ssm的汽车租赁出租系统(源代码+数据库+Lw文档)
    LabVIEW定义自定义错误代码在应用程序中使用
    删除docker容器日志
    SpringBoot SpringBoot 开发实用篇 3 测试 3.7 匹配响应体【JSON】
  • 原文地址:https://blog.csdn.net/m0_68976043/article/details/133241343