顺序队列

/**
 * 顺序队列模拟
 *
 * @Author: gb.wang
 * @Date 2020/5/18 13:54
 * @Version 1.0
 */
public class ArrayQueue<T> {

    // 定义一个数组
    private Object[] arr;

    // 对头指针
    private int front;

    //队尾指针
    private int rear;

    public ArrayQueue(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException("容量不能小于0!");
        }
        arr = new Object[capacity];
        //初始化对头 和队尾指针
        this.front = 0;
        this.rear = 0;
    }

    /**
     * 显示队列所有的数据
     */
    public void display() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空!");
        }
        for (int i = 0; i < rear; i++) {
            System.err.println(i + "----->" + arr[i]);
        }
    }

    /**
     * 往队列里面插入数据
     *
     * @param t 插入数据
     */
    public void insert(T t) {
        // 先判断下数组是否是满了
        if (isFull()) {
            throw new RuntimeException("队列已满!");
        }
        arr[rear] = t;
        rear++;
    }


    /**
     * 出对
     */
    public T poll() {
        if (isEmpty()) {
            throw new RuntimeException("没有可出队的数据!");
        }
        return (T) arr[front++];
    }

    /**
     * 判断队列是否为空。
     */
    public boolean isEmpty() {
        // 如果头和尾指针相同就是空队列
        return this.rear == this.front;
    }

    /**
     * 判断数组是否满了
     */
    public boolean isFull() {
        // 如果数组的length 等于rear,那么数组就满了
        return this.rear == arr.length;
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值