数组实现队列 详细讲解(java)

本文详细介绍了如何使用数组来模拟实现一个队列,并通过银行排队场景展示了队列的应用。在初始的数组队列基础上,提出了其局限性,即不能复用,随后引入了环形数组的概念,通过取模操作优化了数组队列,提高了空间利用率。文章提供了完整的Java代码实现,包括队列的初始化、入队、出队、显示队头数据和队列状态检查等功能,并给出了交互式的操作示例。
摘要由CSDN通过智能技术生成

数组实现队列 详细讲解

队列的介绍

在这里插入图片描述
在这里插入图片描述

队列的一个使用场景

银行排队的案例
在这里插入图片描述

数组模拟队列的思路:

在这里插入图片描述

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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

舒克日记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值