当前位置: 移动技术网 > IT编程>开发语言>Java > java数据结构和算法——数组模拟环形队列(queue)

java数据结构和算法——数组模拟环形队列(queue)

2020年07月17日  | 移动技术网IT编程  | 我要评论

摘要:
由于数组模拟队列,数组无法复用,下面讲解了用数组模拟环形队列的方法,采用取模的方式,使得数组可以重复使用。

一、数组模拟队列的图解

数组模拟队列具体示例参考:https://wwwxz.blog.csdn.net/article/details/107348409
在这里插入图片描述

二、使用数组模拟环形队列的图解

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

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

在数组模拟队列的基础上做出如下调整:

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

四、数组模拟环形队列示例代码

1)、数组模拟环形队列类

package com.rf.springboot01.dataStructure;
/**
 * @description: 使数组模拟环形队列类
 * @author: xiaozhi
 * @create: 2020-07-15 11:22
 */
public class CircleArrayQueue {

    private int maxSize;//表示数组队列的最大容量
    private int front;//front就指向队列的第一个元素
    private int rear;//rear指向队列的最后一个元素的后一个位置
    private int[] arr;//该数据用于存放数据,模拟队列

    //创建环形队列的构造函数
    public CircleArrayQueue(int arrMaxSize){
            maxSize=arrMaxSize;
            arr=new int[arrMaxSize];
    }

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

    //判断队列是否为空
    public boolean isEmpty(){
        return front==rear; //队列头部指针==队列尾部指针,说明队列为空
    }

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

    //获取队列数据,出队列
    public int getQueue(){
        //判断队列是否为空
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能从队列获取数据。");
        }
        int value=arr[front];//先把front对应的值保留到一个临时变量
        front=(front+1)%maxSize;//将front后移,考虑取模
        return value;//将临时保存的变量返回
    }
    //求出当前队列有效数据的个数
    public int size(){
        return  (rear+maxSize-front)%maxSize;
    }

    //显示队列中所有数据
    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 headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列是空的,没有数据。");
        }
        return arr[front];
    }

}

2)、使数组模拟环形队列测试类

package com.rf.springboot01.dataStructure;
import java.util.Scanner;
/**
 * @description: 使数组模拟环形队列测试类
 * @author: xiaozhi
 * @create: 2020-07-15 14:04
 */
public class CircleArrayQueueTest {
    public static void main(String[] args) {
        //设置4,其队列的有效数据最大是3
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
        Scanner scanner = new Scanner(System.in);
        char key=' ';//接收用户输入
        boolean f=true;
        while(f){
            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'://显示队列
                    circleArrayQueue.showQueue();
                    break;
                case 'a'://添加数据到队列
                    System.out.println("请输入一个数:");
                    int value=scanner.nextInt();
                    circleArrayQueue.addQueue(value);
                    break;
                case 'g'://从队列中获取数据
                    try {
                        int result = circleArrayQueue.getQueue();
                        System.out.printf("从队列中取出的数据是%d\n",result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h'://显示队列头部数据
                    try {
                        int result = circleArrayQueue.headQueue();
                        System.out.printf("显示队列的头部数据是%d\n",result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e'://退出队列
                    scanner.close();
                    f=false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

五、运行测试类,效果图如下:

数组模拟环形队列解决了数组模拟队列中不能复用的问题
在这里插入图片描述
在这里插入图片描述

本文地址:https://blog.csdn.net/li1325169021/article/details/107356176

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网