java数组实现环形队列

分析图解

在这里插入图片描述

代码实现

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue queue = new CircleArrayQueue(3);
        try (Scanner scanner = new Scanner(System.in)) {
            String operation;
            A:
            while (true) {
                System.out.println("add: 往队列中添加元素");
                System.out.println("poll: 删除队列头元素并返回头元素");
                System.out.println("ele: 获取队列头部元素");
                System.out.println("print: 打印队列中元素");
                System.out.println("size: 获取队列有效元素个数");
                System.out.println("exit: 退出程序");
                System.out.println("请输入操作");
                operation = scanner.next();
                switch (operation) {
                    case "add":
                        System.out.println("请输入一个数");
                        int data = scanner.nextInt();
                        queue.add(data);
                        break;
                    case "poll":
                        try {
                            int poll = queue.poll();
                            System.out.println("删除的队列头元素为:" + poll);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case "ele":
                        int element = queue.element();
                        System.out.println("队列头元素为:" + element);
                        break;
                    case "print":
                        queue.printQueue();
                        break;
                    case "size":
                        System.out.println("队列有效数据个数:" + queue.size());
                        break;
                    case "exit":
                        break A;
                    default:
                        break;
                }

            }
        }
        System.out.println("程序退出");
    }
}

class CircleArrayQueue {
    private int head;  // 指向队列头部,初始值0
    private int tail; // 指向队尾的后一个位置,初始值0
    private int capacity; // 队列最大容量+1,数组最大容量,1表示总有一个位置作为约定不存储元素
    private int[] elements;

    public CircleArrayQueue(int capacity) {
        // 将客户端指定的容量加1,可以消除约定空间不能放元素的歧义
        // 如果用户指定容量为4,却只能添加3个元素,岂不草蛋
        this.capacity = capacity + 1;
        elements = new int[this.capacity];
    }

    public boolean isFull() {
        return (tail + 1) % capacity == head;
    }

    public boolean isEmpty() {
        return tail == head;
    }

    public int size() {
        return (tail - head + capacity) % capacity;
    }

    /**
     * 添加元素到队列
     *
     * @param data 元素数据
     */
    public void add(int data) {
        if (isFull()) {
            System.out.println("队列满,不能添加数据");
            return;
        }
        elements[tail] = data;
        tail = (tail + 1) % capacity;
    }

    /**
     * 删除队列头
     *
     * @return 已删除的队列头部元素
     */
    public int poll() {
        if (isEmpty()) {
            throw new UnsupportedOperationException("队列为空,不能删除");
        }
        int temp = elements[head];
        head = (head + 1) % capacity;
        return temp;
    }

    /**
     * 检索队列头部元素,但不删除
     *
     * @return 队列头部元素
     */
    public int element() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,没有数据");
        }
        return elements[head];
    }

    /**
     * 打印队列中元素
     */
    public void printQueue() {
        for (int i = head; i < head + size(); i++) {
            // 防止i超出capacity-1导致下标越界
            System.out.printf("elements[%d] = [%d]\n", i % capacity, elements[i % capacity]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值