队列的应用场景和实现

队列特点

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

队列的两端都"开口",要求数据只能从一端进,从另一端出。
在这里插入图片描述

通常,称进数据的一端为 “队尾”,出数据的一端为 “队头”,数据元素进队列的过程称为 “入队”,出队的过程称为 “出队”。

队列中数据的进出要遵循 “先进先出” 的原则,即最先进队列的数据元素,同样要最先出队列

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front 队头)进行删除操作,而在表的后端(rear 队尾)进行插入操作

使用场景

拿排队买票来说,所有的人排成一队,先到者排的就靠前,后到者只能从队尾排队等待,队中的每个人都必须等到自己前面的所有人全部买票成功并从队头出队后,才轮到自己买票。这就不是典型的队列结构吗?

数组模拟队列

  • 队列本身就是有序列表,若使用数组的结构来存储队列的话,则队列数组的声明如下图,其中maxSize是该队列的最大容量。
    在这里插入图片描述

  • 因为队列的输出,输入是从前后端来处理的,因此需要设置两个变量front与rear分别记录队列前后端的下标,front会随着数据输出而改变,rear会随着数据输入而改变。

  • 当我们数据存入队列的时候称为 addQuene
    思路分析
    1、将尾指针往后移: rear+1,当front == rear【空】
    2、若尾指针rear小于队列的最大下标 maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear=maxSize - 1[队列满]。

代码实现

package queue;




import java.util.Arrays;


public class ArrayQueueTest {

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(5);
        arrayQueue.addQueue(6);
        arrayQueue.addQueue(8);


        System.out.println("当前队列元素个数:"+arrayQueue.queueLength());
        System.out.println("队列第一个元素:" + arrayQueue.headQueue());

        arrayQueue.showQueue();

        System.out.println("出队:" + arrayQueue.getQueue());
        System.out.println("出队:" + arrayQueue.getQueue());
        arrayQueue.showQueue();
        System.out.println("当前队列元素个数:"+arrayQueue.queueLength());
    }
}

//使用数组模拟队列,   编写arrayQueue的类
class ArrayQueue{
    //数组最大容量
    private int maxSize;
    //队列头
    private int front;
    //队列尾
    private int rear;
    // 存放数据
    private int[] arr;

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


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

    /**
     * 队列为空返回true,否则false
     * @return
     */
    public boolean isEmpty(){
        return rear == front;
    }

    /**
     * 若队列长度小于maxSize,添加n
     * @param n
     */
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列满了,加不进去了");
            return;
        }
        //队列+1
        rear++;
        arr[rear] = n;
    }

    /**
     * 出队列中队头元素
     * @return
     */
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列已经没有数据了");
        }
        front++;
        int n =  arr[front];
        arr[front] = 0; //设置默认值0
        return  n;
    }

    /**
     * 显示队列所有数据
     */
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列没有数据");
            return;
        }
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 显示队列的头数据
     * @return
     */
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列已经没有数据了");
        }
        return arr[front +1];
    }

    /**
     * 获取队列元素个数
     * @return
     */
    public int queueLength(){
        return rear  -front;
    }
}


问题分析并优化

  • 目前数组使用一次就不能用,没有达到复用的效果,会造成假溢出。
  • 将这个数组使用算法,改进成一个环形的队列取模:%

在这里插入图片描述

环形队列

对前面的数组模拟队列的优化,充分利用数组.因此将数组看做是一个环形的。(通过取模的方式来实现即可)。
分析说明:

  • 尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定,这个在做判断队列满的时候需要注意(rear + 1) % maxSize == front满]
  • rear == front[空]

分析示意图:
在这里插入图片描述

思路

  • front变量的含义做一个调整:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素。front的初始值=0

  • rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置 .因为希望空出一个空间做为约定。rear的初始值=0

  • 队列满时,条件是(rear + 1) % maxSize = front 取模是为了整合rear与front大小为一个的问题

  • 对队列为空的条件,rear == front空

  • 当我们这样分析,队列中有效的数据的个数 (rear- front + maxSize) %maxSize

  • 我们就可以在原来的队列上修改得到,一个环形队列

环形队列实现

package queue;




import java.util.Arrays;


public class ArrayCircleQueueTest {

    public static void main(String[] args) {
        ArrayCircleQueue arrayQueue = new ArrayCircleQueue(5); //队列长度为5,有效数据个数为4
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(5);
        arrayQueue.addQueue(6);
        arrayQueue.addQueue(8);


        System.out.println("队列第一个元素:" + arrayQueue.headQueue());

        arrayQueue.showQueue();

        System.out.println("出队:" + arrayQueue.getQueue());
        System.out.println("出队:" + arrayQueue.getQueue());
        arrayQueue.addQueue(8);
        arrayQueue.addQueue(88);
    }
}

//使用数组模拟队列,   编写arrayQueue的类
class ArrayCircleQueue{
    //数组最大容量
    private int maxSize;
    //队列头
    private int front;
    //队列尾
    private int rear;
    // 存放数据
    private int[] arr;

    //创建队列构造器
    public ArrayCircleQueue(int arrMaxSize){

        this.maxSize = arrMaxSize;

        arr = new int[this.maxSize];
        //front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素。front的初始值=0
        front = 0;
        //rear指向队列的最后一个元素的后一个位置 .因为希望空出一个空间做为约定。rear的初始值=0
        rear = 0;
    }


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

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

    /**
     * 添加
     * @param n
     */
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列满了,加不进去了");
            return;
        }
        //数据直接加入
        arr[rear] = n;
        //rear后移,必须考虑取模
        rear = (rear + 1) % maxSize;
        System.out.println("入队:"+ n);
    }

    /**
     * 出队列
     * @return
     */
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列已经没有数据了");
        }
        //需要分析出 front是指向队列的第一个元素
        //front对应的值保留到一个临时变量
        int n = arr[front];
        //将front后移
        front = (front + 1) % maxSize;
        return n;
    }

    /**
     * 显示队列所有数据
     */
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列没有数据");
            return;
        }
        System.out.println("全部元素" + Arrays.toString(arr));
        System.out.print("队列有效元素:");
        for (int i = front; i <  front + size(); i++) {
            System.out.printf("%4d",arr[i % maxSize]);
        }
        System.out.println();
    }

    /**
     * 显示队列的头数据
     * @return
     */
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列已经没有数据了");
        }
        return arr[front];
    }

    /**
     * 当前队列有效数据个数
     * @return
     */
    public int size(){
        return (rear - front + maxSize) % maxSize;
    }
}

输出
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值