栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构:队列之循环队列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构:队列之循环队列

背景

循环队列出队和入队的均摊复杂度均为O(1)级别,性能比普通队列更高
普通队列的入队复杂度为O(1),但出队复杂度为O(n),因为当出队时,后面的数组有个前移一位的过程.

实现原理 语言描述

实现原理其实也比较简单,就是数组维护队首front和队尾tail索引.当出队时,无需移动数据位置.当入队时,到达数组底部后,通过取余回到数组前端.索引类似在一个数组环上移动,这也是循环一名的由来.
当front和tail相同时,即代表队列为空.当tail的下一个为front时,即代表队列已满,需要扩容.

代码实现
package xyz.yq.ds.queue;


@SuppressWarnings("all")
public class LoopQueue implements Queue {

    
    E[] data;

    private int front = 0;
    private int tail = 0;
    private int size = 0;

    
    public LoopQueue(int capacity) {
        data = (E[]) new Object[capacity + 1];
    }

    public LoopQueue() {
        this(10);
    }


    public int getCapacity() {
        //因为会浪费掉一个空间,实际容纳的数据量要-1
        return data.length - 1;
    }

    @Override
    public int getSize() {
        return size;
    }

    
    @Override
    public boolean isEmpty() {
        return tail == front;
    }

    
    public boolean isFull() {
        return (tail + 1) % data.length == front;
    }

    
    @Override
    public void enqueue(E e) {
        if (isFull()) {
            //扩容为实际能承载的数据的2倍
            resize(getCapacity() * 2);
        }
        //入队,将尾节点先后移动一位即可.由于可能超出队列长度,故取队列长度的余数,让内存可以循环利用
        this.data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    
    @Override
    public E dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from a empty queue.");
        }

        E ret = data[front];
        data[front] = null;
        size--;
        front++;

        //出队后可能不需要那么大空间了,此时进行缩容操作
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }

        return ret;
    }

    
    private void resize(int newCapacity) {
        //因为会浪费掉一个空间,实际容纳的数据量要-1
        E[] newData = (E[]) new Object[newCapacity + 1];
        //从0到size-1处即可
        for (int i = 0; i < size; i++) {
            //新数组数据是从0开始
            newData[i] = data[(i + front) % data.length];
        }

        data = newData;
        front = 0;
        tail = size;
    }

    @Override
    public E getFront() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from a empty queue.");
        }
        return data[front];
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size=%d , capacity=%d n", size, getCapacity()));
        res.append("front [");
        //语义方式
//        for (int i = front; i != tail; i = (i + 1) % data.length) {
//            res.append(data[i]);
//            if ((i + 1) % data.length != tail) {
//                res.append(",");
//            }
//        }

        //偏移量方式
        for (int i = 0; i < size; i++) {
            res.append(data[(i + front) % data.length]);
            if (i != size - 1) {
                res.append(",");
            }
        }
        res.append("] tail");
        return res.toString();
    }

    public static void main(String[] args) {
        LoopQueue queue = new LoopQueue<>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);
            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891455.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号