4. 基于自定义数组的环形队列实现

package com.lonely.队列;

/**
 * @author ztkj-hzb
 * @Date 2019/6/18 15:46
 * @Description 循环队列
 */
public class LoopQueue<T> implements Queue<T> {

    /**
     * 队列数据
     */
    private T[] datas;

    /**
     * 开始的指针
     */
    private int front;

    /**
     * 末尾的下一个的指针
     */
    private int tail;

    /**
     * 队列中实际长度
     */
    private int size;

    public LoopQueue() {
        this(10);
    }

    public LoopQueue(int capcity) {
        //故意多留一格,是用于区分 front == tail 是相等还是满了的情况
        this.datas = (T[]) new Object[capcity + 1];
        front = tail = size = 0;
    }

    /**
     * 入队
     *
     * @param t
     */
    @Override
    public void enqueue(T t) {
        //判断是否已经满了
        if (this.front == (this.tail + 1) % this.datas.length) {
            //队列已满,扩容
            resize(2 * getCapcity());
        }

        //赋值
        this.datas[tail] = t;
        this.tail = (tail + 1) % this.datas.length;
        size++;

    }


    /**
     * 出队
     *
     * @return
     */
    @Override
    public T dequeue() {

        //判断是否没有数据了
        if (this.isEmpty()) {
            throw new RuntimeException("都没数据了,没法出队!");
        }

        //将队首的指针向后移动
        T frontData = this.datas[front];
        front = (front + 1) % this.datas.length;
        size--;

        //判断是否需要缩容
        if (getSize() <= getCapcity() / 2) {
            resize(getCapcity() / 2);
        }

        return frontData;
    }

    /**
     * 获取队首的数据
     *
     * @return
     */
    @Override
    public T getFront() {
        return this.datas[front];
    }

    /**
     * 获取队列实际长度
     *
     * @return
     */
    @Override
    public int getSize() {
        return this.size;
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }

    /**
     * 格式化输出
     *
     * @return
     */
    @Override
    public String toString() {

        StringBuilder message = new StringBuilder("【LoopQueue front{");

        for (int i = 0; i < getSize(); i++) {
            T t = this.datas[(front + i) % this.datas.length];
            message.append(t);
            if (i != getSize() - 1) {
                message.append(",");
            }
        }
        message.append("}tail】");
        return message.toString();
    }

    /**
     * 扩容
     *
     * @param newCapcity
     */
    private void resize(int newCapcity) {
        T[] newDatas = (T[]) new Object[newCapcity + 1];
        for (int i = 0; i < getSize(); i++) {
            newDatas[i] = this.datas[(front + i) % this.datas.length];
        }
        this.datas = newDatas;
        this.front = 0;
        this.tail = getSize();
    }


    /**
     * 获取实际容量
     *
     * @return
     */
    @Override
    public int getCapcity() {
        return this.datas.length - 1;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值