02队列及代码实现

队列介绍

  1. 队列是一个有序列表,可以用数组或是链表来实现。、
  2. 遵循先入先出的原则。 (先存入队列的数据,要先取出。后存入的要后取出)

在这里插入图片描述

数组模拟队列

  1. 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明图如下,其中maxSize是该队列的最大容量。

  2. 因为队列的输入、输出分别是前后端来处理,因此需要两个变量front及rear分别记录队列前后端的下标,front会随着数据输出而改变,而rear则是随着数据输入而改变,具体如图。
    在这里插入图片描述

    当我们将数据存入队列时称为“addQueue”,addQueue的处理需要有两个步骤:(思路分析)

    1. 将尾指针向后移动 rear+1,当front==rear【空】
    2. 若尾部指针rear小于队列的最大下标maxSize -1 ,则将数据存入rear所指的数组元素中,否则无法存入数据。rear == maxSize -1【队列满】

第一版代码:

package com.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        // 写一个循环控制用户操作

        char key = ' ';
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单列表
        while (loop){
            System.out.println("s:  显示队列");
            System.out.println("e:  退出程序");
            System.out.println("a:  添加元素到队列");
            System.out.println("g:  从队列取出数据");
            System.out.println("h:  获取队列头的数据");
            key = sc.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;

                case 'a':
                    System.out.println("输入你要添加的数");
                    int num = sc.nextInt();
                    arrayQueue.addQueue(num);
                    break;
                case 'g':
                    try {
                        System.out.println("取出数据为"+ arrayQueue.getQueue());
                    }catch (Exception e){
                        //TODO 处理异常
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("该队列头数据为"+arrayQueue.peekQueue());
                    }catch (Exception e){
                        //TODO 处理异常
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
            }
        }
        System.out.println("再见~~~");
    }
}

//使用数组模拟队列  编写一个类Queue
class ArrayQueue {
    private int maxSize;//表示数组的最大容量
    private int front;//头指针
    private int rear;//尾指针

    private int[] arr;//数组存放数据,完成队列结构

    //创建构造器
    //构造器中对队列进行初始化
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[this.maxSize];
        //指向队列头部  分析指向队列头的前一个位置
        this.front = -1;
        //指向队列尾部  指向队列尾的数据(即包含队列最后一个数据)
        this.rear = -1;
    }

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

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

    //添加数据(进队列)
    public void addQueue(int element) {
        if (isFull()) {
            System.out.println("队列满,不能加入数据");
            return;
        }
        rear++;
        arr[rear] = element;
    }

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

    //展示当前队列
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空,无遍历内容");
        }
        for (int i = front + 1; i < rear+1; i++) {
            System.out.printf("arr[%d]=%d\t", i, arr[i]);
        }
    }

    //获取队列第一个元素,和出队列不同
    public int peekQueue() {
        if (isEmpty()) {
            //如果为空抛出异常
            throw new RuntimeException("队列空");
        }
        return arr[front + 1];
    }
}

问题分析

  1. 当前的数组使用一次之后就不能使用了
  2. 将这个数组使用算法搞成环形数列

优化成环形数组

思路如下:(这只是一种思路,也可以根据自己的思路进行、宗旨就是将数组头尾连接成为一个环形。)

  1. front变量的含义有所调整:front就指向队列的第一个元素,也就是说arr[front] 就是队列的第一个元素
  2. rear变量的含义:rear指向队列的最后一个元素的后一个位置。希望空出一个空间作为一个约定
  3. 当队列满时, (rear + 1) % maxSize == front
  4. 当队列空时,rear == front(与原先没有变化)。
  5. 当这样规定以后队列中有效的数字个数为(rear - front + maxSize) % maxSize
  6. 根据这个思路我们可以在这个基础上进行修改,得到环形队列。
  7. front和rear的初始值都是0。

代码实现

import java.util.Scanner;

//通过取模的方式实现环形列表
public class CircleQueueDemo {
    public static void main(String[] args) {
        System.out.println("-------测试数组完成环形队列的案例--------");
        //创建一个队列 这里设置最大大小为4但是实际使用大小为3
        CircleQueue circleQueue = new CircleQueue(4);
        // 写一个循环控制用户操作
        char key = ' ';
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单列表
        while (loop) {
            System.out.println("s:  显示队列");
            System.out.println("e:  退出程序");
            System.out.println("a:  添加元素到队列");
            System.out.println("g:  从队列取出数据");
            System.out.println("h:  获取队列头的数据");
            key = sc.next().charAt(0);
            switch (key) {
                case 's':
                    circleQueue.showQueue();
                    break;

                case 'a':
                    System.out.println("输入你要添加的数");
                    int num = sc.nextInt();
                    circleQueue.addQueue(num);
                    break;
                case 'g':
                    try {
                        System.out.println("取出数据为" + circleQueue.getQueue());
                    } catch (Exception e) {
                        //TODO 处理异常
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("该队列头数据为" + circleQueue.peekQueue());
                    } catch (Exception e) {
                        //TODO 处理异常
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
            }
        }
        System.out.println("再见~~~");
    }
}

class CircleQueue {
    private int maxSize;//表示数组的最大容量
    private int front;//头指针
    private int rear;//尾指针

    private int[] arr;//数组存放数据,完成队列结构

    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        //todo 因为判断条件的改变这里在用户使用时传入的参数最好自行加1
        this.arr = new int[this.maxSize];
        //指向队列头部
        this.front = 0;
        //指向队列尾部  指向队列尾的数据  尾部的后面要空出一个空间作为约定
        this.rear = 0;
    }

    // 获取当前队列的有效元素个数。
    public int getSize() {
        return (rear - front + maxSize) % maxSize;
    }

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

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

    //添加数据(进队列)
    public void addQueue(int element) {
        if (isFull()) {
            System.out.println("队列满,不能加入数据");
            return;
        }
        arr[rear] = element;
        //rear后移一位所以要考虑是否需要重头开始的问题。
        rear = (rear + 1) % maxSize;
    }

    //获取数据(出队列)
    public int getQueue() {
        if (isEmpty()) {
            //如果为空抛出异常
            throw new RuntimeException("队列空,不能获取数据");
        }
        // 这里需要分析出front指向的是第一个元素,
        // 先把front对应的值存放在临时变量中,
        // 然后将front后移动  后移的时候要考虑移动位置
        int res = arr[front];
        front = (front + 1) % maxSize;
        return res;//不需要将原来的位置置空,后期的数据直接覆盖了就行了。
    }

    //展示当前队列
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空,无遍历内容");
        }
        //这一部分的遍历方式是肯定不对了
        //for (int i = front + 1; i < rear + 1; i++) {
        //    System.out.printf("arr[%d]=%d\t", i, arr[i]);
        //}
        // 先思考 从?开始遍历,遍历?个有效元素
        for (int i = front; i < front + getSize(); i++) {
            System.out.printf("arr[%d] = %d\t", i % maxSize, arr[i % maxSize]);
        }
        System.out.println();

    }

    //获取队列第一个元素,和出队列不同
    public int peekQueue() {
        if (isEmpty()) {
            //如果为空抛出异常
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黎丶辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值