(四)数据结构-队列

命运对每个人都是一样的,不一样的是各自的努力和付出不同,付出的越多,努力的越多,得到的回报也越多,在你累的时候请看一下身边比你成功却还比你更努力的人,这样,你就会更有动力。

导读:本篇文章讲解 (四)数据结构-队列,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

  1. 队列

1.1 队列介绍

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

  • 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出。

  • 示意图

(四)数据结构-队列

1.2 数组模拟队列

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

  • 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front 及 rear 分别记录队列前后端的下标,front会随着数据输出而变化,而 rear 则是随着数据输入而改变,如下图所示。

(四)数据结构-队列

  • 当我们将数据存入队列时称为”addQueue”,addQueue的处理需要有两个步骤:

  1. 将为指针往后移:rear+1,当 front==rear【空】

  1. 若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear 所指的数据元素中,否则无法存入数据。rear == maxSize-1【队列满】

代码实现:

/**
 * 数组模拟队列
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        // 创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        // 接收用户输入
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        // 输出一个菜单
        while (loop){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):查看队列头的数据");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数字");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
        System.out.println("退出程序");
    }
}

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

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

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

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

    // 添加数据到队列
    public void addQueue(int n) {
        // 判断队列是否满
        if (isFull()) {
            System.out.println("队列满,不能加入数据~");
            return;
        }
        // 让 rear 后移
        rear++;
        arr[rear] = n;
    }

    // 获取队列的数据,出队列
    public int getQueue() {
        // 判断队列是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取出数据~");
        }
        front++;
        return arr[front];
    }

    // 显示队列的所有数据
    public void showQueue() {
        // 遍历数据
        if (isEmpty()) {
            System.out.println("队列为空,无数据~");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    // 显示队列的头数据,注意不是取出数据
    public int headQueue() {
        // 判断队列是否空
        if (isEmpty()) {
            System.out.println("队列为空,无数据~");
            throw new RuntimeException("队列为空,不能读取数据~");
        }
        return arr[front + 1];
    }

}

问题分析及优化:

(1)目前数组使用一次就不能用了,没有达到复用的效果(队列数据全部取出后,不能进行新增);

(2)将这个数组使用取模算法,改进成一个环形的数组(使用取模:% 来实现)。

1.3 数组模拟环形队列

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

关于取模运算的特点与应⽤

对于取模(取余)运算%,⽐如A % M,结果永远都是在[0, M-1)之间循环,并且如果A < M,则结果和没有进⾏取模运算⼀样。这⼀特点有很多应⽤场景

1、最常见的就是对2取模来判断奇偶数;

2、循环队列中通过对最⼤容量取模来控制数组下标,防⽌索引越界;

分析说明:

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

  1. 队列为空判断条件 rear==front

使用数组模拟环形队列的思路分析:

思路如下:

(1)front变量的含义做一个调整:front指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0;

(2)rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear的初始值为0;

(3)当队列满时,条件为 (rear+1)%maxSize == front,想比较上述模拟过程发生了变化;

(4)当队列为空的条件,front == rear;

(5)队列中有效的数据个数为:(rear+maxSize-front)%maxSize

(四)数据结构-队列

根据环形队列的思路,对数组模拟队列的代码进行优化,以下为优化后的代码实现

/**
 * 数组模拟环形队列
 */
public class ArrayAnnularQueueDemo {
    public static void main(String[] args) {
        // 创建一个队列
        // 队列的有效数据为 maxSize-1
        ArrayAnnularQueue arrayQueue = new ArrayAnnularQueue(4);
        // 接收用户输入
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        // 输出一个菜单
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):查看队列头的数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数字");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
        System.out.println("退出程序");
    }
}

// 使用数组模拟环形队列-编写一个ArrayAnnularQueue类
class ArrayAnnularQueue {
    // 数组最大容量
    private int maxSize;
    // front指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0
    private int front;
    // rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear的初始值为0
    private int rear;
    // 该数组用于存放数据
    private int[] arr;

    // 创建队列的构造器
    public ArrayAnnularQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        // 指向队列头部
        front = 0;
        // 指向队列尾部
        rear = 0;
    }

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

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

    // 添加数据到队列
    public void addQueue(int n) {
        // 判断队列是否满
        if (isFull()) {
            System.out.println("队列满,不能加入数据~");
            return;
        }
        // 直接加入数据
        arr[rear] = n;
        // 将 rear 后移
        rear = (rear + 1) % maxSize;
    }

    // 获取队列的数据,出队列
    public int getQueue() {
        // 判断队列是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取出数据~");
        }
        // 先把front对应的值保留到一个临时变量
        int data = arr[front];
        // 将front后移
        front = (front + 1) % maxSize;
        // 返回值
        return data;
    }

    // 显示队列的所有数据
    public void showQueue() {
        // 遍历数据
        if (isEmpty()) {
            System.out.println("队列为空,无数据~");
        }
        // 从 front开始遍历,遍历多少个元素

        for (int i = front; i < front + size(); i++) {

            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    // 求出当前数据有效数据个数
    public int size(){
        return (rear + maxSize - front) % maxSize;
    }

    // 显示队列的头数据,注意不是取出数据
    public int headQueue() {
        // 判断队列是否空
        if (isEmpty()) {
            System.out.println("队列为空,无数据~");
            throw new RuntimeException("队列为空,不能读取数据~");
        }
        return arr[front];
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/142590.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!