循环队列 和 链表队列

在这里插入图片描述

/**
 * 功能:循环队列
 * 创建时间:2022/9/18 22:15
 */
public class LoopQueue<E> implements Queue<E> {

    private E[] data;
    //起始索引位置,末尾索引位置
    private int front, tail;
    //元数个数
    private int size;

    public LoopQueue() {
        this(10);
    }

    public LoopQueue(int capacity) {
        this.data = createArray(capacity);
    }

    public int getCapacity() {
        //循环链表有一个空格子,这样保证循环比较好判断是否含有数据及顺序性
        return data.length - 1;
    }

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

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

    @Override
    public void enqueue(E element) {
        //使用%时怕超过容量,这样可以再次循环
        if ((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }
        //tail是元素末尾位置,刚好是接下来要存入元素位置
        data[tail] = element;
        tail = (tail + 1) % data.length;
        size++;

    }

    @Override
    public E dequeue() {
        if (isEmpty())
            throw new IndexOutOfBoundsException("该队列为空。。。");

        E element = data[front];
        data[front] = null;
        this.front = (front + 1) % data.length;
        this.size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }
        return element;
    }

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

    @SuppressWarnings("unchecked")
    private E[] createArray(int Capacity) {
        return (E[]) new Object[Capacity + 1];
    }

    @Override
    public E getFront() {
        if (isEmpty())
            throw new IndexOutOfBoundsException("该队列为空。。。");
        return data[front];
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Queue: size = ").append(size).append(" , capacity = ").append(getCapacity()).append("\n");
        builder.append("    front [");
        //循环整个数组
        for (int i = front; i != tail; i = (i + 1) % data.length) {
            builder.append(data[i]);
            if ((i + 1) % data.length != tail)
                builder.append(", ");
        }
        builder.append("] tail");
        return builder.toString();
    }

    public static void main(String[] args) {
        LoopQueue<Integer> loopQueue = new LoopQueue<>();
        for (int i = 0; i < 10; i++) {
            loopQueue.enqueue(i);
            System.out.println(loopQueue);

            if (i % 3 == 2) {
                loopQueue.dequeue();
                System.out.println(loopQueue);
            }

        }
    }

结果:

Queue: size = 1 , capacity = 10    front [0] tail
Queue: size = 2 , capacity = 10    front [0, 1] tail
Queue: size = 3 , capacity = 10    front [0, 1, 2] tail
Queue: size = 2 , capacity = 5     front [1, 2] tail
Queue: size = 3 , capacity = 5     front [1, 2, 3] tail
Queue: size = 4 , capacity = 5     front [1, 2, 3, 4] tail
Queue: size = 5 , capacity = 5     front [1, 2, 3, 4, 5] tail
Queue: size = 4 , capacity = 5     front [2, 3, 4, 5] tail
Queue: size = 5 , capacity = 5     front [2, 3, 4, 5, 6] tail
Queue: size = 6 , capacity = 10    front [2, 3, 4, 5, 6, 7] tail
Queue: size = 7 , capacity = 10    front [2, 3, 4, 5, 6, 7, 8] tail
Queue: size = 6 , capacity = 10    front [3, 4, 5, 6, 7, 8] tail
Queue: size = 7 , capacity = 10    front [3, 4, 5, 6, 7, 8, 9] tail

二、链表队列

在这里插入图片描述

package queue;


/**
 * 功能:链表实现队列
 * 创建时间:2022/9/24 21:33
 */
public class LinkedListQueue<E> implements Queue<E> {

    private int size;
    //队列头和尾
    private Node head, tail;

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

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void enqueue(E e) {
        if (isEmpty()) {
            head = new Node(e, null);
            tail = head;
        } else {
            //插入到尾部,因为尾部没有前一个节点的索引,所以只能做插入操作
            tail.next = new Node(e, null);
            tail = tail.next;
        }
        size++;
    }

    @Override
    public E dequeue() {
        if (isEmpty())
            throw new RuntimeException("队列为空。。。。。。");
        Node result = head;
        head = head.next;
        result.next = null;
        if(null == head)
            tail = null;
        size--;
        return result.element;
    }

    @Override
    public E getFront() {
        E result;
        if (isEmpty()) {
            throw new RuntimeException("队列为空。。。。。。");
        } else {
            result = head.element;
        }
        return result;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Queue ").append("size=").append(size).append(" front [");
        for (Node currentNode = head; currentNode != null; currentNode = currentNode.next)
            builder.append(currentNode.element).append("->");
        builder.append("NULL").append(" ] tail");
        return builder.toString();
    }

    private class Node {
        E element;
        Node next;

        public Node() {
            this(null, null);
        }

        public Node(E element, Node next) {
            this.element = element;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        LinkedListQueue<Integer> queue = new LinkedListQueue<>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);

            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }

        }
    }
}

结果:

Queue size=1 front [0->NULL ] tail
Queue size=2 front [0->1->NULL ] tail
Queue size=3 front [0->1->2->NULL ] tail
Queue size=2 front [1->2->NULL ] tail
Queue size=3 front [1->2->3->NULL ] tail
Queue size=4 front [1->2->3->4->NULL ] tail
Queue size=5 front [1->2->3->4->5->NULL ] tail
Queue size=4 front [2->3->4->5->NULL ] tail
Queue size=5 front [2->3->4->5->6->NULL ] tail
Queue size=6 front [2->3->4->5->6->7->NULL ] tail
Queue size=7 front [2->3->4->5->6->7->8->NULL ] tail
Queue size=6 front [3->4->5->6->7->8->NULL ] tail
Queue size=7 front [3->4->5->6->7->8->9->NULL ] tail
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值