Java泛型实现环形队列

CircularQueue.java
package com.example.demo.circularqueue;

/**
 * @author sunweidong
 */
public class CircularQueue<T> {

    /**
     * 默认环形队列的容量,默认3600个元素
     */
    public static final int DEFAULT_CAPACITY = 3600;

    /**
     * 队列头部
     */
    private Integer head;

    /**
     * 队列尾部
     */
    private Integer tail;

    /**
     * 队列容量
     */
    private Integer capacity;

    /**
     * 队列元素数组
     */
    private T[] elements;

    /**
     * 构造一个给定容量的环形队列
     *
     * @param capacity 环形队列的容量
     */
    public CircularQueue(Integer capacity) {
        // 默认头部元素设置为数组的第0号元素
        this.head = 0;
        // 默认尾部元素设置为数组的第0号元素
        this.tail = 0;
        this.capacity = capacity;
        this.elements = (T[]) new Object[capacity];
    }

    /**
     * 构造一个默认容量的环形队列,默认容量3600
     */
    public CircularQueue() {
        // 默认头部元素设置为数组的第0号元素
        this.head = 0;
        // 默认尾部元素设置为数组的第0号元素
        this.tail = 0;
        this.capacity = DEFAULT_CAPACITY;
        this.elements = (T[]) new Object[DEFAULT_CAPACITY];
    }

    /**
     * 判断队列是否已满
     *
     * @return boolean
     */
    public boolean isFull() {
        // 头部和尾部没有指向同一个元素 并且 队列的元素个数和初始化大小相同
        return !this.head.equals(this.tail) && this.size().equals(this.capacity);
    }

    /**
     * 判断队列是否为空
     *
     * @return boolean
     */
    public boolean isEmpty() {
        // 头部和尾部没有指向同一个元素
        return this.head.equals(this.tail);
    }

    /**
     * 添加一个队列元素
     *
     * @param t 队列元素
     */
    public void add(T t) {
        // 判断队列是否已满
        if (isFull()) {
            throw new RuntimeException("环形队列已满");
        }
        // 1.尾部对容量求余
        // 2.设置元素内容
        // 3.尾部向后移动一个角标
        this.elements[this.tail % this.capacity] = t;
        this.tail++;
    }

    /**
     * 删除一个队列头部元素
     */
    public void remove() {
        // 判断队列是否已空
        if (isEmpty()) {
            throw new RuntimeException("环形队列无元素");
        }
        // 1.头部元素置为空
        // 2.头部向后移动一个角标
        this.elements[this.head % this.capacity] = null;
        this.head++;
    }

    /**
     * 获取一个队列头部元素
     */
    public T get() {
        // 判断队列是否已空
        if (isEmpty()) {
            throw new RuntimeException("环形队列无元素");
        }
        // 获取头部元素
        return this.elements[this.head % this.capacity];
    }

    /**
     * 获取一个队列头部元素
     */
    public T getAndRemove() {
        // 获取一个头部元素,并临时存储
        T t = this.get();
        // 删掉头部元素
        this.remove();
        // 返回数据
        return t;
    }

    /**
     * 获取队列元素个数
     *
     * @return 队列容量
     */
    public Integer size() {
        return this.tail - this.head;
    }

}
QueueTest.java
package com.example.demo.circularqueue;

/**
 * @author sunweidong
 */
public class QueueTest {

    public static void main(String[] args) {
        add();
        // addAndError();
        remove();
    }

    private static void add() {
        CircularQueue<String> strQueue = new CircularQueue<>();
        for (int i = 0; i < CircularQueue.DEFAULT_CAPACITY; i++) {
            strQueue.add(i + "");
        }
        System.out.println("isFull=" + strQueue.isFull());
        System.out.println("isEmpty=" + strQueue.isEmpty());
        System.out.println("size=" + strQueue.size());
        System.out.println();
    }

    private static void addAndError() {
        CircularQueue<Integer> intQueue = new CircularQueue<>(100);
        for (int i = 0; i < 100; i++) {
            intQueue.add(i);
        }
        System.out.println("isFull = " + intQueue.isFull());
        System.out.println("isEmpty = " + intQueue.isEmpty());
        System.out.println("size = " + intQueue.size());
        System.out.println();
        // 环形队列已满
        intQueue.add(100);
    }

    private static void remove() {
        CircularQueue<String> strQueue = new CircularQueue<>();
        for (int i = 0; i < CircularQueue.DEFAULT_CAPACITY; i++) {
            strQueue.add(i + "");
        }
        System.out.println("add---isFull = " + strQueue.isFull());
        System.out.println("add---isEmpty = " + strQueue.isEmpty());
        System.out.println("add---size = " + strQueue.size());
        System.out.println();
        for (int i = 0; i < CircularQueue.DEFAULT_CAPACITY; i++) {
            strQueue.remove();
        }
        System.out.println("remove---isFull = " + strQueue.isFull());
        System.out.println("remove---isEmpty = " + strQueue.isEmpty());
        System.out.println("remove---size = " + strQueue.size());

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值