ArrayQueue源码学习

基本定义

ArrayQueue 是一个循环队列(Queue),继承了 AbstractList 抽象类,内部通过数组的方式来实现,同时 增加 和 删除 元素不会引起内部数组的拷贝。

	private int capacity;
    private T[] queue;
    private int head;
    private int tail;

构造函数

	public ArrayQueue(int capacity) {
        this.capacity = capacity + 1;
        this.queue = newArray(capacity + 1);
        this.head = 0;
        this.tail = 0;
    }
    
    private T[] newArray(int size) {
        return (T[]) new Object[size];
    }

capacity+1是因为初始化时,队列内没有元素,此时 head=0; tail=0; ,当有一个元素添加进去后,tail 就要向后偏移一位(会有一个位移差),在这个容器中需要多一位来存放这个 tail 指针。
同时newArray(capacity + 1)也表明本队列实际存储形式为数组。

扩容

由于数组大小是提前确定的,也无法自动扩容(可以调用 resize() 进行手动扩容),因此不太适合当前队列个数未知的情况。

	public void resize(int newcapacity) {
        int size = size();
        if (newcapacity < size)
            throw new IndexOutOfBoundsException("Resizing would lose data");
        //为尾指针预留一个空间来保存
        newcapacity++;
        if (newcapacity == this.capacity)
            return;
        T[] newqueue = newArray(newcapacity);
        for (int i = 0; i < size; i++)
        	//重写了get()方法,实现了从原队列的头部获取数据
            newqueue[i] = get(i);
        this.capacity = newcapacity;
        this.queue = newqueue;
        this.head = 0;
        this.tail = size;
    }
    //重写了size方法
	public int size() {
        // Can't use % here because it's not mod: -3 % 2 is -1, not +1.
        int diff = tail - head;
        if (diff < 0)
            diff += capacity;
        return diff;
    }

可以通过如下代码和下方的动图定性了解diff += capacity的操作

		ArrayQueue<Integer> numQueue = new ArrayQueue<>(4);
        numQueue.add(1);
        numQueue.add(2);
        numQueue.add(3);
        numQueue.add(4);
        System.out.println("numqueue:"+ numQueue);
        System.out.println("numqueue:"+numQueue.size());
        numQueue.remove(0);
        numQueue.remove(0);
        numQueue.remove(0);

        System.out.println("numqueue:"+ numQueue);
        System.out.println("numqueue:"+ numQueue.size());

添加与删除

	public boolean add(T o) {
        queue[tail] = o;
        //通过取模的方式获得下一个尾指针所指空间的下标,通过这种方式双向循环了起来
        int newtail = (tail + 1) % capacity;
        //如果新尾指针与头指针指向同一个位置,则说明ArrayQueue已经满了抛越界异常,如有需要手动resize扩容
        if (newtail == head)
            throw new IndexOutOfBoundsException("Queue full");
        //更新尾指针
        tail = newtail;
        return true; // we did add something
    }
	//输入的i为0时(且无缺省值),移除数组队列的队首元素
    public T remove(int i) {
        if (i != 0)
            throw new IllegalArgumentException("Can only remove head of queue");
        if (head == tail)
            throw new IndexOutOfBoundsException("Queue empty");
        //取得队首元素
        T removed = queue[head];
        queue[head] = null;
        //更新头指针
        head = (head + 1) % capacity;
        //返回被移除的元素
        return removed;
    }

附上循环队列插入和移除的动态图链接,形象理解head与tail在队列中的表现形式。

https://pic2.zhimg.com/v2-e8087668d545e668c70c969a28a26c95_b.webp

因为队列是循环队列,所以在进行入队、出队操作时,就不能像顺序队列那样对tail、head进行简单的加1操作,我们需要对tail、head加1后与数组的大小进行求余操作,来得出tail、head的值,这样才能进行循环操作。循环队列需要牺牲一个存储空间,对于一个存储空间为n的循环队列来说只能存放n-1为数据(呼应前方newArray(capacity + 1)),因为如果不牺牲一个存储空间的话,当tail==head时,就有可能存在队空或者队满的情况。

查询

	//获取队首元素
	public T get(int i) {
        int size = size();
        if (i < 0 || i >= size) {
            final String msg = "Index " + i + ", queue size " + size;
            throw new IndexOutOfBoundsException(msg);
        }
        int index = (head + i) % capacity;
        return queue[index];
    }

引用资料

1、https://zhuanlan.zhihu.com/p/80523559
2、https://blog.csdn.net/love667767/article/details/80365068#commentsedit

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值