栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java数据结构与算法---队列(三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java数据结构与算法---队列(三)

基本介绍

  队列是一个有序列表,可以用数组或者链表来实现,并且遵循先入先出的原则,即:先存入队列的数据要先去出来,后存入的要后取出。

数组模拟队列

  队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图,其中maxSize是该队列的最大容量;因为队列的输入输出分别是从前后端来处理的,因此需要两个变量 front 和 rear 分别记录队列前后端的下标,front会随着数据输入而改变,而rear则是随着数据的输入而改变,如下图

思路分析

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

  1. 将尾指针往后移:rear + 1,当 front == rear
  2. 若尾指针rear小于队列的最大下表maxSize - 1,则将数据存入rear所指的数组元素中,否则无法存入数据。
代码实现

  定义一个用数组实现的队列

public class ArrayQueue {
    
    private Integer maxSize;
    
    private Integer front;
    
    private Integer rear;
    
    private int[] arr;

    
    public ArrayQueue(Integer maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        this.front = -1;
        this.rear = -1;
    }

    
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    
    public boolean isEmpty() {
        return rear.equals(front);
    }

    
    public void addQueue(int n) {
        // 判断队列是否满了
        if (isFull()) {
            System.out.println("队列满了, 添加失败");
            return;
        }
        rear++;
        arr[rear] = n;
    }

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

    
    public void showQueue() {
        // 遍历数组
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d] = %dn", i, arr[i]);
        }
    }

    
    public Integer headQueue() {
        // 判断是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列没数据");
        }
        return arr[front + 1];
    }
}

  测试该类的使用

public class ArrayQueueDemo {
    public static void main(String[] args) {
        // 创建队列
        ArrayQueue queue = 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): 查看队列头的数据");
            System.out.print("请输入: ");
            key = scanner.next().charAt(0); // 接收一个字符
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    System.out.print("请输入一个数: ");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        System.out.println("获取到队列数据: " + queue.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("获取到队列头数据: " + queue.headQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~~");
    }
}

  上面的数组队列实际上还是存在问题的,例如队列满了,并且其中的数据也取完了,是不能再次进行存入数据的,因此该数组队列没有达到复用的效果。我们可以通过一个算法,将它改造成一个环形的数组

数组模拟环形队列
思路分析

  1. front 变量的含义调整为:front就指向队列的第一个元素,即 arr[front]就是队列的第一个元素,front初始值为0
  2. rear变量的含义调整为:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间做为约定,rear的初始值为0
  3. 队列满的条件是:(rear + 1) % maxSize = front
  4. 队列空的条件是:rear == front
  5. 队列的有效数据个数是:(rear + maxSize - front) % maxSize

代码实现

  定义数组实现环形队列

public class CircleArrayQueue {
    
    private Integer maxSize;
    
    private Integer front;
    
    private Integer rear;
    
    private int[] arr;

    
    public CircleArrayQueue(Integer maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        this.front = 0;
        this.rear = 0;
    }

    
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    
    public boolean isEmpty() {
        return rear.equals(front);
    }

    
    public void addQueue(int n) {
        // 判断队列是否满了
        if (isFull()) {
            System.out.println("队列满了, 添加失败");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    
    public Integer getQueue() {
        // 判断队里是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空, 不能取数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    
    public void showQueue() {
        // 遍历数组
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d] = %dn", i % maxSize, arr[i % maxSize]);
        }
    }

    
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    
    public Integer headQueue() {
        // 判断是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列没数据");
        }
        return arr[front];
    }
}

  测试环形队列

public class ArrayQueueDemo {
    public static void main(String[] args) {
        // 数组模拟环形队列
        circleArrayQueueDemoTest();
    }

    
    public static void circleArrayQueueDemoTest() {
        // 创建队列
        CircleArrayQueue queue = new CircleArrayQueue(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): 查看队列头的数据");
            System.out.print("请输入: ");
            key = scanner.next().charAt(0); // 接收一个字符
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    System.out.print("请输入一个数: ");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        System.out.println("获取到队列数据: " + queue.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("获取到队列头数据: " + queue.headQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~~");
    }
}

  源码地址:https://gitee.com/peachtec/data-structure-and-algorithm/tree/master/src/com/hxz/dataStructure/queue

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1040961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号