数据结构--数组模拟环形队列

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

该文章主要解决上一篇博客(数据结构–数组模拟队列)留下来的队列不能重复使用的问题
在这里插入图片描述

代码实现

import java.util.Scanner;

public class CircleArrayQueueDemo {

    public static void main(String[] args) {
        //说明:设置maxSize为3,其队列的有效数据最大是2,原因:要空出一个空间作为约定
        CircleArrayQueue queue = new CircleArrayQueue(3);
        boolean flag = true;
        while(flag){
            System.out.println("请选择你要执行的操作,add:进队  get:出队  print:打印队列  quit:结束当前程序  head:查看队列头数据 count:计算有效元素个数");
            Scanner sc = new Scanner(System.in);
            String next = sc.next();
            switch(next){
                case "add":
                    queue.addQueue();
                    break;

                case "get":
                    try{
                        int res = queue.getQueue(queue);
                        System.out.println("当前出队元素为:" + res);
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;

                case "print":
                    queue.printQueue(queue);
                    break;

                case "quit":
                    flag = false;
                    break;

                case "head":
                    try{
                        int re = queue.headQueue();
                        System.out.println("队列头的数据是:"+re);
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;

                case "count":
                    int r = queue.size();
                    System.out.println("有效元素个数为:"+r+"个");
                    break;

                default:
                    break;
            }
        }
    }
}

//使用数组模拟队列,编写一个ArrayQueue类
class CircleArrayQueue {
    private int maxSize;  //表示数组的最大容量

    //指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素
    //front的初始值=0
    private int front;

    //rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定
    //rear的初始值=0
    private int rear;

    private static int[] array; //该数组用于存放数据,模拟对列


    //创建队列的构造器
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.front = 0;  //指向队列头部,分析出font是指向队列头的前一个位置
        this.rear = 0;  //指向队列尾,指向队列尾的数据(即就是队列最后一个数据)
        this.array = new int[maxSize];
    }


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

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


    //添加数据到队列
    public void addQueue() {

        if (isFull()) {
            System.out.println("队列已满,不能再往里面添加数据");
            return;
        }

        System.out.println("请输入进队元素的值:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        //直接将数据加入
        array[rear] = num;
        //将rear后移,这里必须考虑取模
        rear = (rear + 1) % maxSize;
    }


    //获取队列的数据,出队列
    public int getQueue(CircleArrayQueue aq) {

        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取数据");
        }

        //这里需要分析出front是指向队列的第一个元素
        //1.先把front对应的值保存到一个临时变量
        //2.将front后移,考虑取模
        //3.将临时保存的变量返回
        int value = array[front];
        front = (front + 1) % maxSize; //font后移
        return value;
    }


    //显示队列的所有数据
    public void printQueue(CircleArrayQueue aq) {
        if (isEmpty()) {
            System.out.println("队列为空,没有数据");
        }

        //思路:从front开始遍历,遍历多少个元素
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n",i % maxSize, aq.array[i % maxSize]);
        }
    }


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

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



结果截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值