三、数据结构与算法 (一)数组模拟队列

本文通过Java代码展示了如何使用数组实现一个简单的队列,包括初始化队列、添加元素、遍历队列及取出元素。队列是一种先进先出的数据结构,常用于处理排队问题,如核酸检测排队、银行排队等。代码中定义了队列的满和空的判断,并提供了相应的操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.代码

1.QueueExample2

public class QueueExample2 {
    public static void main(String[] args) {
        MyArrayQueue myArrayQueue = new MyArrayQueue(3);
        System.out.println("原始数组遍历:");
        myArrayQueue.showQueue();
        System.out.println("添加三个值后遍历");
        myArrayQueue.addQueue(1);
        myArrayQueue.addQueue(2);
        myArrayQueue.addQueue(3);
        myArrayQueue.showQueue();
        System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
        System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
        myArrayQueue.showQueue();
    }

    private static class MyArrayQueue {
        int maxSize;
        int rear=-1;
        int front=-1;
        private  int[] arr;

        /**
         *
         * @param max 表示队列的最大长度
         */
        public MyArrayQueue(int max) {
            maxSize=max;
            arr = new int[max];//设置长度
        }
        //遍历数组
        public void showQueue() {
            System.out.println("遍历数组");
            for (int i : arr) {
                System.out.println(i);
            }
        }
        //添加数组数据
        public void addQueue(int nextInt) {

            if (isFull()) {//判断队列是否已经满了
                System.out.println("数组最大长度为3,不会再添加");
            }else {
                arr[rear+1]=nextInt;
                rear++;
            }
            System.out.println("数组的长度:"+rear);
        }
        //取出数据
        public int takeOutQueue() {
            if (isEmpty()) {
               throw new RuntimeException("队列为空,不能取数据");
            }
            int i = arr[front+1];
            arr[front+1]=0;
            front++;
            return i;
        }

        /**
         * 队列是否已经满了
         * @return true表示已经满了,false表示队列还没有满
         */
        public boolean isFull(){
            return rear==maxSize-1;
        }

        /**
         * 队列是否为空
         * @return true表示队列为空,false表示队列不为空
         */
        public boolean isEmpty(){
            return rear==front;
        }

    }
}

运行结果:
在这里插入图片描述

2.讲解

1.队列

1.介绍

是个先进先出的有序列表,可以用数组、链表来实现。(感觉就是为了解决排队这种问题而设置出来的东西,服务于需求)

2.应用

应用核酸检测排队,打新冠疫苗排队,电影排队,厕所排队,银行排队……

2.图解

1.图

下图是一个三步骤过程:创建空队列–>添加4个数据后的队列–>删除2个后的队列
在这里插入图片描述

1.解析图

只加不减。rear表示队列的后部,front表示队列的头部
在这里插入图片描述

3.代码讲解

其实就是用数组实现过程:创建队列(展示),添加数据(展示),取出数据(展示)

MyArrayQueue myArrayQueue = new MyArrayQueue(3);
        MyArrayQueue myArrayQueue = new MyArrayQueue(3);
        System.out.println("原始数组遍历:");
        myArrayQueue.showQueue();
        System.out.println("添加三个值后遍历");
        myArrayQueue.addQueue(1);
        myArrayQueue.addQueue(2);
        myArrayQueue.addQueue(3);
        myArrayQueue.showQueue();
        System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
        System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
        myArrayQueue.showQueue();
1.创建队列,然后展示

创建队列对象

MyArrayQueue myArrayQueue = new MyArrayQueue(3);
构造方法

```java
        int maxSize;
        int rear=-1;
        int front=-1;
        private  int[] arr;
        /**
         * 
         * @param max 表示队列的最大长度
         */
        public MyArrayQueue(int max) {
            maxSize=max;
            arr = new int[max];//设置长度
        }

展示

myArrayQueue.showQueue();
//遍历数组
        public void showQueue() {
            System.out.println("遍历数组");
            for (int i : arr) {
                System.out.println(i);
            }
        }
2.添加数据,然后展示

这里要进行判断队列是否已经满了

System.out.println("添加三个值后遍历");
        myArrayQueue.addQueue(1);
        myArrayQueue.addQueue(2);
        myArrayQueue.addQueue(3);
        myArrayQueue.showQueue();
//添加数组数据
        public void addQueue(int nextInt) {
            if (isFull()) {//判断队列是否已经满了
                System.out.println("数组最大长度为3,不会再添加");
            }else {
                arr[rear+1]=nextInt;
                rear++;
            }
            System.out.println("数组的长度:"+rear);
        }

//队列是否已经满了

/**
         * 队列是否已经满了
         * @return true表示已经满了,false表示队列还没有满
         */
        public boolean isFull(){
            return rear==maxSize-1;
        }
3.取出队列,然后展示,这里要判断是否为空,空的话,是无法获取的
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
        System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
        myArrayQueue.showQueue();

//根据先进先出(取),来获取值

 //取出数据
        public int takeOutQueue() {
            if (isEmpty()) {
               throw new RuntimeException("队列为空,不能取数据");
            }
            int i = arr[front+1];
            arr[front+1]=0;
            front++;
            return i;
        }

判断是否为空,因为队列里的尾部rear,头部front都是只能是加的,那么它们相等的情况为值相等

/**
         * 队列是否为空
         * @return true表示队列为空,false表示队列不为空
         */
        public boolean isEmpty(){
            return rear==front;
        }

3.总结反思

1.效率

其实排队这种增删比较频繁的使用链表会比较好,它增删快。而顺序链表的话是查询快,增删慢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值