Java--算法--队列

队列的应用场景:

  1.  数组模拟队列:

    1. package com.model.queue;
      
      
      import java.util.Scanner;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/24 20:39
       *
       * 数组模拟队列,并验证
       */
      public class ArrayQueueDemo01 {
          public static void main(String[] args) {
      
              ArrayQueue queue=new ArrayQueue(3);
      
              Scanner scanner = new Scanner(System.in);
      
              char key=' ';
      
              Boolean loop=true;
              while (loop){
                  System.out.println("s(show):显示队列");
                  System.out.println("e(exit):退出程序");
                  System.out.println("a(add):添加数据到队列");
                  System.out.println("g(get):从队列中取出数据");
                  System.out.println("h(head):查看队列的头数据");
                  key=scanner.next().charAt(0);
                  switch (key){
                      case 's':
                          queue.showQueue();
                          break;
                      case 'e':
                          scanner.close();
                          loop=false;
                          break;
                      case 'a':
                          System.out.println("请输入一个选择");
                          int n=scanner.nextInt();
                          queue.addQueue(n);
                          break;
                      case 'g':
                          try {
                              int res=queue.getQueue();
                              System.out.printf("取出的数据是%d\n",res);
                          }catch (Exception e){
                              System.out.println(e);;
                          }finally {
      
                          }
                          break;
                      case 'h':
                          try {
                              int head= queue.headQueue();
                              System.out.printf("取出的数据是%d\n",head);
                          }catch (Exception e){
                              System.out.println(e);
                          }finally {
      
                          }
      
                          break;
                      default:
                          break;
      
                  }
      
              }
              System.out.println("退出程序");
          }
      
      }
      class ArrayQueue{
          private int maxSize;  //队列的最大长度
          private int front;   //指向队列头部的前一个
          private int rear;   //指向队列的尾部
          private int[] arr;   //数组模拟队列
      
          public ArrayQueue(int maxSize) {
              this.maxSize = maxSize;
              arr=new int[maxSize];
          }
          boolean isFull(){
              return maxSize-1==rear;
          }
          boolean isEmpty(){
              return rear==front;
          }
      
          //添加元素
          public void addQueue(int n){
              //判断队列是否满了
              if(isFull()){
                  System.out.println("队列已满,不能添加元素");
              }
              rear++;
              arr[rear]=n;
          }
          //获取元素
          public int getQueue(){
              //判断对了是否为空
              if (isEmpty()){
                  throw new RuntimeException();
              }
              front++;
              return arr[front];
          }
      
          //遍历所有的元素
          public void showQueue(){
              if (isEmpty()){
                  System.out.println("队列为空没有数据");
              }
              for (int i = 0; i < arr.length; i++) {
                  System.out.printf("arr[%d]=%d\n",i,arr[i]);
              }
          }
      
          //获得头数据
          public int headQueue(){
              if (isEmpty()){
                  throw new RuntimeException("队列为空");
              }
              return arr[front+1];
          }
      
      }
  2.  数组模拟环形队列

    1. 环形队列,当满足 (rear+1)%maxsize==front时标识队列满,rear后面会预留一个空的位置,所有要+1
    2. 环形队列中rear走的格数是大于等于front的 :(maxsize+rear-front)%maxsize : 当rear大于front是:rear-front    ==  (rear+maxsize-front)%maxsize 、当rear小于front事, maxsize-(front-rear)==(rear+maxsize-front)%maxsize 
    3. 综上所述:(rear+maxsize-front)%maxsize
    4. package com.model.queue;
      
      import java.util.Scanner;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/1 23:40
       *
       * 数组模拟环形队列
       */
      public class ArrayQueueDemo02 {
          public static void main(String[] args) {
              ArrayQueue02 queue02 = new ArrayQueue02(6);
              useCircleQueue(queue02);
      
          }
          public static void useCircleQueue(ArrayQueue02 queue02){
              Scanner scanner = new Scanner(System.in);
              char key=' ';
              boolean loop=true;
              while (loop){
                  System.out.println("s(show):显示队列");
                  System.out.println("e(exit):退出程序");
                  System.out.println("a(add):添加数据到队列");
                  System.out.println("g(get):从队列中取出数据");
                  System.out.println("h(head):查看队列的头数据");
                  key=scanner.next().charAt(0);
                  switch (key){
                      case 'a':
                          try {
                              System.out.println("请输入一个数据,加入到队列中");
                              int value = scanner.nextInt();
                              queue02.add(value);
                          }catch (Exception e){
                              e.printStackTrace();
                          }
                          break;
                      case 'g':
                          try {
                              System.out.printf("取出的队列是:%d\n",queue02.get());
                          }catch (Exception e){
                              e.printStackTrace();
                          }
                          break;
                      case 'e':
                          loop=false;
                          break;
                      case 's':
                          queue02.show();
                          break;
                      case 'h':
                          System.out.printf("队列的头是:%d\n",queue02.headQueue());
                          break;
                      default:
                          break;
      
                  }
                  System.out.println("退出程序~");
      
              }
          }
      
      }
      
      class ArrayQueue02{
      
          private int[] arr;
          private int front=0; //指向第一个数组
          private int rear=0; //指向最后一个数据的后一个位置
          private int maxSize;
      
          public ArrayQueue02(int maxSize) {
              this.maxSize = maxSize;
              this.arr = new int[this.maxSize];
          }
          public void add(int element){
              if(!isFull()){
                  arr[rear]=element;
                  rear=(rear+1)%maxSize;
              }else {
                  throw new RuntimeException("队列已满");
              }
      
      
          }
          public int get(){
              if (!isEmpty()) {
                  int index=front;
                  front=(front+1)%maxSize;
                  return arr[index];
              }else {
                 throw new RuntimeException("队列为空");
              }
          }
          public void show(){
              if (isEmpty()){
                  System.out.println("队列为空,没有数据");
                  return;
              }
              //从front 开始遍历,遍历队列中的元素的个数
              for (int i=front;i<front+size();i++){
                  System.out.printf("arr[%d}\t=%d\n",i%maxSize,arr[i%maxSize]);
              }
          }
          public int headQueue(){
              return arr[front];
          }
          //当前队列的元素个数
          public int size(){
              return (maxSize+rear-front)%maxSize;
          }
          public boolean isFull(){
              return (rear+1)%maxSize==front;
          }
          public boolean isEmpty(){
              return rear==front;
          }
      }
posted @ 2021-06-24 21:51  张紫韩  阅读(63)  评论(0编辑  收藏  举报