首页 > 其他分享 >数据结构之队列

数据结构之队列

时间:2022-12-30 22:33:34浏览次数:30  
标签:队列 System int println 数据结构 rear out

1.队列介绍

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

1672405749512

2.数组模拟队列思路

  1. 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如上图,其中 maxSize是该队列的最大容量。
  2. 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front 及 rear分别记录队列前后端的下标,front会随着数据输出而改变,而rear 则是随着数据输入而改变。
  3. 当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:
    1. 将尾指针往后移: rear+1,当front == rear为空
    2. 若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear-=maxSize - 1[队列满]
package com.yt;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        //测试
        ArrayQueue arrayQueue = new ArrayQueue(10);
        char key = '0';//接收用户输入
        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("程序退出");

    }
}

//使用数组模拟队列
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 = -1;
        rear = -1;//队尾和队头指针初试时都指向相同的位置
    }

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

    //往队列中增加元素
    public void addQueue(int num){
        //先判断是否队列满
        if (isFull()){
            System.out.println("队列满了,不能添加");
            return;
        }
        rear++;//让尾指针后移一位便于存放数据
        arr[rear] = num;
    }

    //删除队列中的数据,并返回删除的数据
    public int getQueue(){
        //先判断队列是否为空
        if (isEmpty()){
//            System.out.println("队列为空,不能再删除了");
//            return -1;
            //抛出异常
            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]=%d\n",i,arr[i]);
        }
    }

    //显示队列的第一个数据,注意不是取出数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front+1];
    }
}

3.数组模拟环形队列

上述实现方式存在只能使用一次的问题,没有达到复用的效果,因此将该数组优化为环形队列

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

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

1672409011504

1672409026638

package com.yt;

import java.util.Scanner;

public class ArrayQueueDemo1 {
    public static void main(String[] args) {
        //测试
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = '0';//接收用户输入
        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("程序退出");

    }
}

//使用数组模拟环形队列
class ArrayQueue1{
    private int maxSize;//表示数组的最大容量
    private int front;//表示队列头,初试值为0
    private int rear;//表示队列尾,初试值为0
    private int[] arr;//该数组用于存放数据,模拟队列

    public ArrayQueue1(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;//队尾和队头指针初试时都指向相同的位置
    }

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

    //往队列中增加元素
    public void addQueue(int num){
        //先判断是否队列满
        if (isFull()){
            System.out.println("队列满了,不能添加");
            return;
        }
        //rear++;//让尾指针后移一位便于存放数据
        //直接将数据加入
        arr[rear] = num;
        //将rear后移,这里必须实现取模操作
        rear = (rear + 1) % maxSize;
    }

    //删除队列中的数据,并返回删除的数据
    public int getQueue(){
        //先判断队列是否为空
        if (isEmpty()){
//            System.out.println("队列为空,不能再删除了");
//            return -1;
            //抛出异常
            throw new RuntimeException("队列为空,不能取数据");
        }
//        front++;
//        return arr[front];
        //先取值再移动
        int value = arr[front];//先将值取出来保存到临时变量
        front = (front + 1) % maxSize;//再将头指针后移
        return value;
    }

    //遍历队列中的所有数据
    public void showQueue(){
        //判断队列是否为空
        if (isEmpty()){
            System.out.println("队列为空,没有数据");
            return;
        }
        //从front开始遍历,遍历多少个元素呢?编写一个计算有效数据个数的方法
        for (int i=front; i<front+size(); i++){
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }

    //计算有效数据个数,重点
    public int size(){
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列的第一个数据,注意不是取出数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}

标签:队列,System,int,println,数据结构,rear,out
From: https://www.cnblogs.com/y-tao/p/17015964.html

相关文章