数组方式构建环形队列 代码实现

package queue;

import java.util.Scanner;

/**
 * 数组方式构建环形队列  代码实现
 * */

public class CircleQueueDemo {
    public static void main(String[] args) {
        //测试一把
        System.out.println("测试数组模拟环形队列的案例~~~");

        // 创建一个环形队列
        CircleQueue queue = new CircleQueue(4); //说明设置4, 其队列的有效数据最大是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':
                    queue.show();
                    break;
                case 'a':
                    System.out.println("输入一个数");
                    int value = scanner.nextInt();
                    queue.addData(value);
                    break;
                case 'g': // 取出数据
                    try {
                        int res = queue.getData();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // 查看队列头的数据
                    try {
                        int res = queue.heafCircleQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // 退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");
    }
}

class CircleQueue {
    private int maxSize;
    private int front; //环形队列头:指向环形队列的第一个元素
    private int rear; //环形队列尾:指向环形队列的最后一个元素的下一个位置
    private int[] circleQueue;  //该数组用于模拟环形队列存储数据

    /**
     * 创建环形队列的时候,需要动态初始化数据
     * */
    public CircleQueue(int length) {
        this.maxSize = length;
        circleQueue = new int[length];
    }

    //判断环形队列是不是满的
    public boolean isFull() {
        // 环形队列什么时候满的,队列的最后一个元素的下一个位置 + 1 = 队列的第一个元素的位置的时候,队列满
        // 如果环形队列的最后一个元素恰好是数组的最大长度,那么+1的时候,就会出现数组越界的问题
        // 因此,要对rear+1的值进行取模,
        // 对谁取模呢?
        // 对数组的最大长度取模,因为,任何小于数组最大长度的值对它进行取模,得到的值都是它本身
        // 大于数组最大长度的值对它进行取模,得到的值是(大于数组最大长度的值 - 数组的最大长度)
        // 等于数组最大长度的值对它进行取模,得到的值是0
        return (rear + 1) % maxSize == front;
    }

    // 判断环形队列是不是空的
    // 当front == rear 的时候,队列为空
    public boolean isEmpty() {
        return front == rear;
    }

    // 向环形队列中添加数据
    public void addData(int i) {
        // 添加数据之前,对环形队列进行判断是不是满了的操作
        if (isFull()){
            System.out.println("队列已经满了,不能再添加数据了");
            return;
        }
        // 向环形队列的最后一个元素添加数据
        circleQueue[rear] = i;
        // 将环形队列的最后一个元素的指针向后移动,因为是数组,且是环形的思想
        // 要考虑指针后移数组越界的问题和当到达环形队列的最后一个位置,要让只能再次回到第一个
        // 因此要对后移指针的位置取模
        rear = (rear + 1) % maxSize;
    }

    // 从环形队列中取出数据
    public int getData() {
        // 首先对环形队列进行判空操作
        if (isEmpty()) {
            throw new RuntimeException("队列为空,没有数据");
        }
        // 取数据:本着先进先出的原则,因此,要移动环形队列的第一个元素的指针
        int value = circleQueue[front]; // 要取出的数据
        // 将环形队列的第一个元素的指针向后移动,因为是数组,且是环形的思想
        // 要考虑指针后移数组越界的问题和当到达环形队列的最后一个位置,要让只能再次回到第一个
        // 因此要对后移指针的位置取模
        front = (front + 1) % maxSize;
        return value;
    }

    // 显示环形队列的所有数据
    public void show() {
        // 对环形队列进行判空操作
        if (isEmpty()) {
            throw new RuntimeException("队列为空,没有数据");
        }
        // 遍历存储数组的数组
        // 问题在于,数组已经进行了动态的初始化,如何规避掉值为0的数据,也就是只取出所有的有效数据
        // 首先:数组的front-rear这中间的数据一定是有效数据,因为取出数据的时候,front指针会移动
        // 添加数据的时候,rear指针也会移动
        // 那么,遍历的时候,开始元素就应该是front了
        // 那么,rear的位置如何确定 (下一个方法,可以确定rear的位置)

        // 新的问题来了
        // if front = 2, rear = 0, maxSize = 3,
        // 这个时候你遍历的是环形队列的有效个数
        // 会出现数组越界的问题
        for (int i = front; i < front + getValidDataCount(); i++) {
            //System.out.printf("arr[%d]=%d\n", i, circleQueue[i]);
            System.out.printf("arr[%d]=%d\n", i % maxSize, circleQueue[i % maxSize]);
        }
    }

    // 获取当前环形队列有效数据(非0数据)的个数
    public int getValidDataCount() {
        // if front = 0,rear = 2,maxSize = 3, 那么有效数据的个数就是两个(想不明白,回过头看新增的方法)
        // if front = 1, rear = 0, maxSize = 3, 这个时候有效数据的个数也是两个,但是有效数据的计算就不能是
        // rear - front了
        // 应该是(rear + maxSize - front) % maxSize取模的值
        // 这样得到的值就是有效数据的个数了
        return (rear + maxSize - front) % maxSize;
    }

    // 显示环形队列的头数据
    public int heafCircleQueue() {
        // 对环形队列进行判空操作
        if (isEmpty()) {
            throw new RuntimeException("队列为空,没有数据");
        }
        return circleQueue[front];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值