数据结构 队列

介绍

概述

先说队列这个家伙长得咋样吧。它就像一个长长的队伍,前面的人往后走,后面的人往前挤,形成了一个有序的序列。这个家伙的特点呢,就是先进先出(First In, First Out)。也就是说,你把东西往里头一塞,要出来的话就得按照顺序排队等着。

优缺点

那队列有啥优缺点呢?优点嘛,就是它可以实现简单的递增操作。比如说,你要把一些东西按顺序放进去,然后又想按照一定的顺序取出来,这时候队列就大显身手了。缺点嘛,就是它的性能相对较差。因为它是按照先进先出的顺序来存储数据的,所以在队尾插入或者删除元素的时候,会导致队列头部和尾部的数据都需要移动,这样效率就不高了。

应用场景

队列的应用场景也是非常广泛的。比如说,我们经常用到的邮件系统就是一个典型的队列应用。邮件系统里头有一个邮筒,你可以把邮件扔进去,然后等待别人来取走。这个邮筒就是一个队列,里面的邮件就是数据。当然了,这只是一个简单的例子,实际上队列在计算机科学中有很多其他的用途,比如操作系统、编译器、数据库等等。

Api设计

ArrayQueue.png

/**
 * @author sssd
 * @careate 2023-07-09-9:19
 */
public interface Queue<E> {

    void enqueue(E e); //进入队列

    E dequeue(); // 出队

    E getFront(); //查看队首

    int getSize(); 

    boolean isEmpty();
}

实现

数组实现

/**
 * @author sssd
 * @careate 2023-07-09-9:25
 */
public class ArrayQueue<E> implements Queue<E>{

    ArrayList<E> arrayList;


    public ArrayQueue(int capacity) {
        arrayList  = new ArrayList<>(capacity);
    }

    public ArrayQueue() {
        arrayList  = new ArrayList<>();
    }


    @Override
    public void enqueue(E e) {
        arrayList.addLast(e);
    }

    @Override
    public E dequeue() {
        return arrayList.removeFirst();
    }

    @Override
    public E getFront() {
        return arrayList.getFirst();
    }

    @Override
    public int getSize() {
        return arrayList.getSize();
    }

    @Override
    public boolean isEmpty() {
        return arrayList.isEmpty();
    }
}

循环队列

LoopQueue.png

/**
 * @author sssd
 * @careate 2023-07-14-14:58
 */
public class LoopQueue<E> implements Queue<E> {

    private E[] data;
    private int front, tail;
    private int size;

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

    public LoopQueue() {
        this(10);
    }

    public int getCapacity() {
        return data.length - 1;
    }


    @Override
    public void enqueue(E e) {
        if ((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    @Override
    public E dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("queue is empty...");
        }
        E removeElement = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }
        return removeElement;
    }

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

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

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

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity + 1];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(1 + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    @Override
    public String toString() {
        return "LoopQueue{" +
                "data=" + Arrays.toString(data) +
                ", front=" + front +
                ", tail=" + tail +
                ", size=" + size +
                '}';
    }
}

作者:傻傻三多

出处:https://www.sssd.top/archives/1688033050884

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

本文由博客一文多发平台 OpenWrite 发布!


sssd
1 声望0 粉丝