数组模拟环形队列实现

队列介绍

  • 队列是一个有序列表,可以用数组或者链表来实现。
  • 遵循先入先出原则。即:先存入队列的数据要先取出,后存入的要后取出。

数组模拟队列

  • 当我们将数据存入队列时称为add,add的处理需要有两个步骤:
    1. 将尾指针往后移:rear + 1,当 front == rear 时队列为空
    2. 若尾指针rear小于队列的最大小标maxSize - 1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear == maxSize - 1 时队列满

代码实现

public class ArrayQueueDemo {
   public static void main(String[] args) {
       ArrayQueue arrayQueue = new ArrayQueue(3);
       char key = ' ';
       Scanner scanner = new Scanner(System.in);
       boolean loop = true;
       while (loop){
           System.out.println("s(show): 显示队列");
           System.out.println("a(add): 添加数据到队列");
           System.out.println("g(get): 取出队列数据");
           System.out.println("h(head): 显示队列头数据");
           System.out.println("q(quit): 退出程序");
           key = scanner.next().charAt(0);
           switch (key){
               case 's':
                   arrayQueue.show();
                   break;
               case 'a':
                   System.out.println("输入一个数");
                   arrayQueue.add(scanner.nextInt());
                   break;
               case 'g':
                   try {
                       int value = arrayQueue.get();
                       System.out.printf("取出的数据是%d\n",value);
                   } catch (Exception e) {
                       System.out.println(e.getMessage());
                   }
                   break;
               case 'h':
                   try {
                       int value = arrayQueue.showHead();
                       System.out.printf("队列头数据是%d\n",value);
                   } catch (Exception e) {
                       System.out.println(e.getMessage());
                   }
                   break;
               case 'q':
                   loop = false;
                   break;
               default:
                   System.out.println("输入有误,请重新输入");
                   break;
           }
       }
   }
}

// 使用数组模拟队列
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];
       front = -1; // 指向队列头部,分析出front是指向队列头的前一个位置
       rear = -1;  // 指向队列尾,指向队列尾的数据(就是队列最后一个数据)
   }

   // 判断队列是否满
   public boolean isFull(){
       return rear == maxSize - 1;
   }

   // 判断队列是否为空
   public boolean isEmpty(){
       return rear == front;
   }

   // 添加数据到队列
   public void add(int n){
       // 判断队列是否满
       if(isFull()){
           System.out.println("队列已满");
           return;
       }
       rear++;
       arr[rear] = n;
   }

   // 获取队列的数据,出队列
   public int get(){
       if(isEmpty()){
           throw new RuntimeException("队列为空,不能取数据");
       }
       front++;
       return arr[front];
   }

   public void show(){
       //遍历
       if(isEmpty()){
           System.out.println("队列为空");
       }
       for (int i = front + 1; i <= rear; i++) {
           System.out.printf("arr[%d]=%d\n",i,arr[i]);
       }
   }

   public int showHead(){
       if(isEmpty()){
           System.out.println("队列为空");
       }
       return arr[front+1];
   }
}
  • 问题分析并优化
    • 目前数组使用一次就不能再使用,没有达到复用效果
    • 将这个数组使用算法,改进成一个环形队列

数组模拟环形队列

思路分析

  1. front变量的含义调整:front就指向队列的第一个元素,即arr[front]就是队列的第一个元素,front初始值 = 0
  2. rear变量的含义调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear初始值 = 0
  3. 当队列满时,条件是(rear + 1) % maxSize = front
  4. 当队列空时,条件是front == rear
  5. 队列中有效的数据的个数 (rear + maxSize - front) % maxSize

代码实现

public class CircleArrayQueueDemo {
    public static void main(String[] args) {   
        CircleArrayQueue queue = new CircleArrayQueue(4);
        char key;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("s(show): 显示队列");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 取出队列数据");
            System.out.println("h(head): 显示队列头数据");
            System.out.println("q(quit): 退出程序");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    queue.show();
                    break;
                case 'a':
                    System.out.println("输入一个数");
                    queue.add(scanner.nextInt());
                    break;
                case 'g':
                    try {
                        int value = queue.get();
                        System.out.printf("取出的数据是%d\n",value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int value = queue.showHead();
                        System.out.printf("队列头数据是%d\n",value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'q':
                    loop = false;
                    break;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }
}

class CircleArrayQueue{

    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public CircleArrayQueue(int maxSize){
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = 0;
        rear = 0;
    }

    public boolean isFull(){
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty(){
        return rear == front;
    }

    public void add(int n){
        if(isFull()){
            System.out.println("队列已满");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    public int get(){
        if(isEmpty()){
            throw new RuntimeException("队列已空");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public void show(){
        if(isEmpty()){
            System.out.println("队列已空");
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    public int size(){
        return (rear + maxSize - front) % maxSize;
    }
    
    public int showHead(){
        if(isEmpty()){
            throw new RuntimeException("队列已空");
        }
        return arr[front];
    }

}
  • 创建 CircleArrayQueue(4) 实际只能存储 3 个元素
  • 这是因为 isFull() 方法使用 (rear + 1) % maxSize == front 判断队满
  • 需要牺牲一个数组位置来区分队空和队满状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还能做什么

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值