数组模拟循环队列

对前面的数组模拟队列的优化,充分利用数组 . 因此将数组看做是一个环形的。 ( 通过 取模的方式来实现 即可 )
1) 尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定 , 这个在做判断队列满的
时候需要注意   (rear + 1) % maxSize == front 满]
2) rear == front [空]
3) 分析示意图 :

代码实现
import java.util.Scanner;
import javax.naming.ldap.LdapReferralException;
public class CircleArrayQueue {
    public static void main(String[] args) {
        //测试一下
        ArrayQueue.Queue q = new ArrayQueue.Queue(10);//创建一个队列,实际队列容量为9
        char key = ' ';//用来接收用户输入
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(getQueue):从队列中移除数据");
            System.out.println("h(headQueue):读取队首数据");
            System.out.println("e(exit):退出队列");
            key = sc.next().charAt(0);//接收一个字符
            switch (key) {
                case 'a':
                    System.out.println("输入一个您想加入的数");
                    int value = sc.nextInt();
                    q.add(value);
                    break;
                case 's':
                    q.listQueue();
                    break;
                case 'g':
                    try {
                        int res = q.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = q.headQueue();
                        System.out.printf("队列的头数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    System.out.println("程序退出");
                    break;
                default:
                    break;
            }
        }
    }
}
class Queue{
    private int MaxSize;//数组最大容量,即队列的容量
    private int front;//头指针
    private int rear;//尾指针
    private int[] arr;//用来存放数据,模拟队列

    //创建队列的构造器,
    public Queue(int MaxSize){
        this.MaxSize=MaxSize;
        this.front=0;//指向队列头部,即指向队列第一个元素
        this.rear=0;//指向队列尾部的后一个元素,空一个出来作为约定,队列的实际最大容量为MaxSize-1
        //这两行可以不写,应为默认值为零
        this.arr=new int[MaxSize];
    }

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

    // 判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }
    // 添加数据到队列
    public void add(int n) {
        // 判断队列是否满
        if (isFull()) {
            System.out.println("队列满,不能加入数据~");
            return;
        }
        //直接将数据加入
        arr[rear] = n;
        //将 rear 后移, 这里必须考虑取模
        rear = (rear + 1) % MaxSize;
    }
    //获取队列数据,出队列
    public int getQueue(){
        //判断队列是否为空
        if(isEmpty()){
            //通过抛出异常
            throw new RuntimeException("队列为空,不能读取数据");
        }
        // 这里需要分析出 front 是指向队列的第一个元素
        // 1. 先把 front 对应的值保留到一个临时变量
        // 2. 将 front 后移, 考虑取模
        // 3. 将临时保存的变量返回
        int value=arr[front];
        front=(front+1)%MaxSize;
        return value;
    }

    //显示队列的所有数据
    public void listQueue(){
        //遍历
        if(isEmpty()){
            System.out.println("队列空,没有数据");
            return;
        }

        for (int i =front; i <front+size(); i++) {
            System.out.printf("arr[%d]=%d\n",i%MaxSize,arr[i%MaxSize]);
        }
    }

    // 求出当前队列有效数据的个数
     public int size() {
         // rear = 2
         // front = 1
         // maxSize = 3
        return (rear + MaxSize - front) % MaxSize;
    }
    //显示队列的头数据,注意不是取出头数据
    public int headQueue(){
        if(isEmpty()){
            throw  new RuntimeException("队列空,没有数据");
        }
        return arr[front];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值