type
status
date
slug
summary
tags
category
icon
password

数组实现队列 详细讲解

队列的介绍

notion image
notion image

队列的一个使用场景

银行排队的案例
notion image

数组模拟队列的思路:

notion image
1、定义一个数组 arr[maxSize] 作为该队列 2、rear定义为队尾指针 3、front定义为对头指针 4、入队addQueue,将队尾指针往后移,rear+1的情况下,要判断队列的元素是否已满,已满的条件为rear=maxSize-1; 5、出队outQueue,将队头的元素往后移,front+1的情况下,要判断队列的元素是否为空,为空的条件为rear=front;

代码实现

package com.qf; import java.util.Scanner; public class ArrayQueue {    private int maxSize;    private int front;    private int rear;    private int[] arr;    /**     * 队列的实现     */    //初始化队列    public ArrayQueue(int maxSize){        this.arr=new int[maxSize];        this.maxSize=maxSize;        this.front=-1;        this.rear=-1;   }    //判断队列是否为空    public boolean isEmpty(){        return front==rear;   }    //判断队列是否已满    public boolean isFull(){        return rear==maxSize-1;   }    //添加数据到队列,入队    public void addQueue(int n){        //判断队列是否已满        boolean full = isFull();        if (full){            System.out.println("队列已满,请稍后~~~");       }else{            arr[rear+1]=n;            rear++;       }   }    //数据从队列中出来,出队    public int  outQueue(){        //判断队列是否为空        boolean empty = isEmpty();        if (empty){            throw new RuntimeException("队列为空,无法取出数据~~~");       }else {            front++;            System.out.println("出队的数据为:"+arr[front]);            arr[front]=0;            return arr[front];       }   }    //队列的展示,遍历数组    public void showQueue(){        //判断队列是否为空        boolean empty = isEmpty();        if (empty){            throw new RuntimeException("队列为空,无法取出数据~~~");       }else{            for (int i : arr) {                System.out.printf(" "+i);           }       }   }    //展示队头数据    public void showHeader(){        System.out.println("队头数据为:"+arr[front+1]);   }    public static void main(String[] args) {        ArrayQueue arrayQueue = new ArrayQueue(4);        boolean loop=true;        char sc=' ';        while (loop){            System.out.println("e 跳出循环");            System.out.println("a 入队");            System.out.println("o 出队");            System.out.println("s 循环队列");            System.out.println("h 展示队头");            Scanner systemPut=new Scanner(System.in);            sc=systemPut.next().charAt(0);            switch (sc){                case 'a':                    System.out.println("请输入数据:");                    Scanner addNum=new Scanner(System.in);                    int num = addNum.nextInt();                    arrayQueue.addQueue(num);                    break;                case 'o':                    arrayQueue.outQueue();                    break;                case 's':                    arrayQueue.showQueue();                    break;                case 'h':                    arrayQueue.showHeader();                    break;                case 'e':                    loop=false;                    break;                default :                    break;           }       }   } }
问题以及需要优化的点
1、目前的数组只能使用一次,不能达到复用的效果 2、将该数组使用算法,改成环形数组,取模%
环形数组详见,我的下一篇博客
https://blog.csdn.net/cativen/article/details/124366550
 
数组实现环形队列 详细讲解普里姆算法