数据结构与算法_队列

队列

队列:先进先出.数据入队列尾指针rear向后移动.数据出队列头指针front向后移动.头指针front和尾指针rear指向数组其实索引前一位也就是 -1

队列空:front = rear
队列满:rear = maxSize - 1

class ArrayQueue {
    private int maxSize; // 队列最大容量

    private int front; // 队列头

    private int rear; // 队列尾

    private int[] arr; // 存储队列数据

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

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

    /**
     * 入队列
     *
     * @param n
     */
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列满不能添加数据!!!");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    /**
     * 出队列
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空不能取数据!!!");
        }
        front++;
        return arr[front];
    }


    /**
     * 返回队列头
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列没有数据");
        }
        return arr[front + 1];
    }
}

环形队列

front 和 rear 初始值 0.rear指向队列中最后元素的后一个位置.也就是队列中空出一个位置
队列空:front = rear
队列满:(rear + 1) % maxSize = front. 该公式就是说当尾节点的下一个节点是头节点. 那么为什么队列中会空出一个位置呢. 因为起始位置index是0.在添加数据时候rear++.也就是说假设一个maxSize为18的数组.如果填满时.此时index就是18.代入公式那么头节点就是1.显然不对.应该是0.所以当空出一个位置时.填满的index就是17.带入公式就是0.也就是头节点所在位置. 有人可能会问那为什么队列满的条件不是 rear%maxSize = front 原因是如果头尾节点还在第一次环中那么和队列空的条件冲突

队列有效元素个数
当 rear > fornt:rear - front
当 rear < fornt:rear + maxSize - front. 实际就是头指针和尾指针之间的元素个数. rear + maxSize - 重复rear长度 - 从其实位置 front - rear 长度也就是 rear + maxSize - front
所以有效元素个数:(rear + maxSize - front )% maxSize

class CircleArray {
    private int maxSize; // 队列最大容量

    private int front; // 队列头

    private int rear; // 队列尾

    private int[] arr; // 存储队列数据

    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

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

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

    /**
     * 入队列
     *
     * @param n
     */
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列满不能添加数据!!!");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 出队列
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空不能取数据!!!");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    /**
     * 查看队列所有数据
     */
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空没有数据");
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d \n", i % maxSize, arr[i % maxSize]);
        }
    }

    /**
     * 获取环形队列中有效元素个数
     *
     * @return
     */
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    /**
     * 返回队列头
     *
     * @return
     */
    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、付费专栏及课程。

余额充值