数据结构学习之环形队列(数组实现)


前言

在上节课的学习中,使用数组实现了单向队列,单向队列存在假溢出的问题,环形队列可以解决这个问题。


一、实现思路

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  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
  6. 牺牲掉一个空间

二、代码实现

代码如下:

package com.atguigu.queue;

import java.util.Scanner;

public class CricleArrayQueueDemo {

    public static void main(String[] args) {
        System.out.println("环形队列演示");
        CricleArray queue = new CricleArray(4); //数组长度为4,但这个队列的有效数据最大是3,需要一个预留空间
        char key = ' ';//接收用户输入
        Scanner scanner = 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 = scanner.next().charAt(0);//接收 一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    System.out.println("输入一个数字");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }
        System.out.println("程序关闭");
    }
}

class CricleArray{
    private int maxSize; //表示数组的最大容量
    //front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
    //front 的初始值 = 0
    private int front;
    //rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
    //rear 的初始值 = 0
    private int rear;
    private int[] arr; //该数据用于存放数据,模拟队列

    public CricleArray(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    //判断队列是否满
    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 + 1) % maxSize;
    }

    //获取队列的数据(出队列)
    public int getQueue(){
        //判断队列是否为空
        if (isEmpty()){
            //为空抛出异常
            throw new RuntimeException("队列为空,不能取数据");
        }
        //这里的front指向队列的第一个元素取出来
        //1  先把front 对应的值保留到一个临时变量
        // 2  将front 后移
        // 3  返回临时变量
        int n = arr[front];
        front = (front + 1)%maxSize;
        return n;
    }

    //显示队列的所有数据
    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 % maxSize,arr[i % maxSize]);
        }
    }

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

    //显示队列的头数据,不是取出数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,没有数据--");
        }
        return arr[front];
    }
}

总结

第一次看这些很懵,不理解是添加数据、去除数据、有效数据是怎样算出来的,多看了几遍,自己动手算了算,稍微理解了一点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值