java数据结构——数组模拟循环队列实现

在这里插入图片描述

  1. 尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定,这个在做判断队列满的时候需要注意 (rear + 1) %
    maxSize == front 满]
  2. rear == front [空]
import java.util.Scanner;

/**
 * 环形数组队列实现
 */
public class CircleArrayQueue {
    private int front;//前指针 默认为0 指向队列的第一个元素
    private int rear;//后指针 默认为0  指向队列的末尾元素的后一位。
    private int maxSize; //数组大小
    private int[] arr;//用于存放队列的数组

    /**
     * 初始化环形队列数组
     *
     * @param maxSize
     */
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    /**
     * 判断数组队列是否满
     *
     * @return
     */
    public boolean ifFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean ifEmpty() {
        return rear == front;
    }

    public void addQueue(int num) {
        if (ifFull()) {
            throw new RuntimeException("队列已满!添加失败!");
        }
        arr[rear] = num;
        rear = (rear + 1) % maxSize;

    }

    public int getQueue() {
        if (ifEmpty()) {
            throw new RuntimeException("队列为空!取出失败");
        }
        int num = arr[front];
        front = (front + 1) % maxSize;
        return num;
    }


    public void showQueue() {
        if(ifEmpty()){
            System.out.println("队列为空,没有数据!");
            return;
        }
        for (int i = front; i < front + ((rear + maxSize - front) % maxSize); i++) {
            System.out.printf("arr[%d]=%d\t", i%maxSize, arr[i%maxSize]);
        }
    }

    public int getFrontNum(){
        if(ifEmpty()){
            throw new RuntimeException("队列为空,没有数据!");
        }
        return arr[front];
    }

}

class Test {
    public static void main(String[] args) {
        boolean flag = true;
        System.out.println("输入【a】:(add)在队列中添加数据 ");
        System.out.println("输入【g】:(get)从队列中取出数据 ");
        System.out.println("输入【s】:(show)显示队列数据 ");
        System.out.println("输入【h】:(head)显示头数据 ");
        System.out.println("输入【e】:(exit)退出程序 ");
        Scanner scanner = new Scanner(System.in);
        CircleArrayQueue queue = new CircleArrayQueue(4);
        while (flag){
            char c = scanner.next().charAt(0);
            switch (c){
                case 'a':
                    System.out.print("请输入要添加的数字:");
                    int i = scanner.nextInt();
                    try {
                        queue.addQueue(i);
                        System.out.println("添加成功");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int  num = queue.getQueue();
                        System.out.println(num);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    queue.showQueue();
                    break;
                case 'h':
                    try {
                        System.out.println(queue.getFrontNum());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    flag=false;
                    scanner.close();
                    break;
                default:
                    break;
            }



        }

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值