Java数组模拟环形队列.

数组模拟环形队列

思路:

1.front变量的含义做一个调整:front指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0;

2.rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置。因为希望空出一个空间作为约定,rear初始值=0;  

3.当队列满时,条件是:(rear+1)%maxSize==front(maxSize永远比rear大,若不在最后一位,除以maxSize也是白除还是rear+1)

4.当队列为空的条件:rear==front

5.当我们这样分析,队列中有效数据的个数:(rear+maxSize-front)%maxSize

package 队列;

import java.util.Scanner;

public class CirlceArrayQueueDemo {
    public static void main(String[] args) {
        CircleArray queue=new CircleArray(5);//长度为5,最大有效数据有四个
        Scanner s=new Scanner(System.in);
        boolean loop=true;
        //输出一个菜单
        while (loop){
            System.out.println("输入1:向队列中增加数据");
            System.out.println("输入2:从队列中取出数据");
            System.out.println("输入3:展示队列中所有数据");
            System.out.println("输入4:展示第一个数");
            System.out.println("输入5:退出程序");
            switch (s.next().charAt(0)){
                case '1':
                    System.out.println("请向队列中增加数据");
                    queue.addQueue(s.nextInt());
                    System.out.println("增加成功");
                    break;
                case '2':
                    System.out.println("从队列中取出数据");
                    try {
                        int result=queue.getQueue();
                        System.out.println("取出成功,取出的数据是:"+result);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case '3':
                    System.out.println("展示队列中所有数据");
                    try {
                        queue.showAll();
                        System.out.println("展示成功!");
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case '4':
                    System.out.println("展示第一个数据");
                    int firstDate=queue.showFirst();
                    System.out.println("第一个数据是:"+firstDate);
                    break;
                case '5':
                    System.out.println("程序退出!");
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出!");
    }
}
class CircleArray{
    private int arrmaxSize;
    private int[] arr;//该数据用于存放数据,模拟队列
    private int rear;//rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置。因为希望空出一个空间作为约定,rear初始值=0;
    private int front;//front变量的含义做一个调整:front指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0;
    public CircleArray(int arrmaxSize){
        this.arrmaxSize=arrmaxSize;
        arr=new int[arrmaxSize];
        front=0;
        rear=0;//默认都为0,可写可不写
    }
    public boolean isFull(){
        return  (rear+1)%arrmaxSize==front;
    }
    public boolean isEmpty(){
        return rear==front;
    }
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列已满");
            return;
        }else{
            arr[rear]=n;
            //将rear后移,这里必须考虑取模
            rear=(rear+1)%arrmaxSize;
        }
    }
    //从队列中取数据(要获取值,所以返回int)
    public int getQueue(){
        if(isEmpty()){
            //如果返回值会不清晰所以抛出异常,抛出异常就不用再返回值的
            throw new RuntimeException("队列为空");
        }
        //这里分析数front是指向队列的第一个元素
        //1.先把front对应的值给一个临时变量
        //2.将front后移,考虑在最后的位置,取模
        //3.将临时变量值返回
        int value=arr[front];
        front=(front+1)%arrmaxSize;
        return value;
    }
    public void showAll(){
        if(isEmpty()){
            System.out.println("队列为空!");
            return;
        }
        for(int i=front;i<front+(rear+arrmaxSize-front)%arrmaxSize;i++){
            System.out.println("arr"+"["+i%arrmaxSize+"]"+"="+arr[i%arrmaxSize]);
        }
    }
    public int showFirst(){
        if(isEmpty()){
            throw new RuntimeException("队列为空!");
        }
        return arr[rear];//只有第一个数
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不想打酱油

谢谢啦!我会加油的!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值