数组队列

106 阅读2分钟

一、队列介绍

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

二、数组模拟队列

package top.snailstudy.queue;

public class ArrayQueue {

    private int maxSize;//表示数组的最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr; //用于存储数据,模拟队列

    //创建队列构造器
    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1; //指向队列头的前一个位置
        rear = -1; //指向队列尾的数据,包含最后一个数据
    }

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

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

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

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

    //显示所有数据
    public void allQueue(){
        if(isEmpty()){
            System.out.println("队列是空的,没有数据");
            return;
        }
        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];
    }

}

注意:出现的问题: 使用一次就不能使用,没有达到复用效果,通过数组模拟环形队列解决。

数组模拟环形队列

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

package top.snailstudy.queue;

public class AnnularArrayQueue {
    private int maxSize;//表示数组的最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr; //用于存储数据,模拟队列

    public AnnularArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

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

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


    //添加数据到队列
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列已满");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    //获取队列的数据,出队列
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        //这里需要分析出 front是指向队列的第一个元素
        //1.先把front对应的值保留到一个临时变量
        //2.将front后移
        //3.将临时保存的变量返回
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }


    //显示所有数据
    public void allQueue(){
        if(isEmpty()){
            System.out.println("队列是空的,没有数据");
            return;
        }
        for (int i = 0; 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 headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列是空的,没有数据~~~");
        }
        return arr[front];
    }
}