队列—普通队列(数组实现)、环形队列(数组实现)、链表实现

队列是很多高级数据结构和算法的基础,比如集合的List、Queue...

以下demo基本包含了队列的基本方法:

普通队列-数组实现 ArrayQueueCommon.java

/**
 * @Author: ltx
 * @Description: 普通队列-数组实现
 */
public class ArrayQueueCommon {
    private int[] arr;
    private int maxSize;//队列大小
    private int head;//头
    private int rear;//尾

    public ArrayQueueCommon(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        head = rear = -1;
    }

    /**
     * 队列是否满了
     *
     * @return
     */
    private Boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判空
     *
     * @return
     */
    private Boolean isEmpty() {
        return rear == head;
    }

    /**
     * 队尾添加元素
     *
     * @param value
     */
    private void add(int value) {
        if (isFull()) {
            System.out.println("队列满了...");
            return;
        }
        arr[++rear] = value;
    }

    /**
     * 队列头移除元素
     *
     * @return
     */
    private int delete() {
        if (isEmpty()) {
            throw new RuntimeException("队列空...");
        }
        return arr[++head];
    }

    /**
     * 遍历队列里面有效数据
     */
    private void show() {
        int i = head;
        while (i < rear) {
            System.out.print(arr[++i] + ", ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayQueueCommon arrayQueue = new ArrayQueueCommon(5);
        arrayQueue.add(2);
        arrayQueue.add(1);
        arrayQueue.add(5);
        arrayQueue.add(3);
        arrayQueue.add(4);
        arrayQueue.add(7);
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        arrayQueue.add(8);
        arrayQueue.add(9);
        arrayQueue.show();
    }
}

环形队列-数组实现 ArrayQueueLoop.java

/**
 * 环形队列-数组实现
 */
public class ArrayQueueLoop {
    private int[] arr;
    private int maxSize;//队列最大长度
    private int head;//头
    private int rear;//尾
    private int size;//队列长度

    /**
     * 初始化
     *
     * @param maxSize
     */
    public ArrayQueueLoop(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    /**
     * 队列是否满了
     *
     * @return
     */
    private Boolean isFull() {
        return size == maxSize;
    }

    /**
     * 判空
     *
     * @return
     */
    private Boolean isEmpty() {
        return size == 0;
    }

    /**
     * 队尾添加元素
     *
     * @param value
     */
    private void add(int value) {
        if (isFull()) {
            System.out.println("队列满了...");
            return;
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
        size++;
    }

    /**
     * 队列头移除元素
     *
     * @return
     */
    private int delete() {
        if (isEmpty()) {
            throw new RuntimeException("队列空...");
        }
        int res = arr[head];
        head = (head + 1) % maxSize;
        size--;
        return res;
    }

    /**
     * 遍历队列里面有效数据
     */
    private void show() {
        int i = 0, j = head;
        while (i < size) {
            i++;
            System.out.print(arr[j] + ", ");
            j = (j + 1) % maxSize;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayQueueLoop arrayQueue = new ArrayQueueLoop(5);
        arrayQueue.add(2);
        arrayQueue.add(1);
        arrayQueue.add(5);
        arrayQueue.add(3);
        arrayQueue.add(4);
        arrayQueue.add(7);
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        arrayQueue.add(8);
        arrayQueue.add(9);
        arrayQueue.show();
    }

}

队列-链表实现 LinkedQuque.java

/**
 * 节点
 */
class SignLinkedNode implements Serializable {
    public Integer value;
    //下一个节点
    public SignLinkedNode next;

    public SignLinkedNode() {
    }

    public SignLinkedNode(Integer value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

/**
 * @Author: ltx
 * @Description: 队列-链表实现
 */
public class LinkedQuque {
    //头指针
    SignLinkedNode head;
    //尾指针
    SignLinkedNode rear;
    //队列实际长度
    int size;
    //最大长度
    int maxSize;

    /**
     * 构造方法-初始化
     *
     * @param maxSize
     */
    public LinkedQuque(int maxSize) {
        this.maxSize = maxSize;
    }

    /**
     * 队列是否满了
     * @return
     */
    private Boolean isFull() {
        return size == maxSize;
    }

    /**
     * 队列判空
     * @return
     */
    private Boolean isEmpty() {
        return size == 0;
    }

    /**
     * 队尾添加元素
     * @param node
     */
    private void add(SignLinkedNode node) {
        if (isFull()) {
            System.out.println("队列满了...");
            return;
        }
        //第一个元素
        if (head == null) {
            head = node;
            rear = node;
        } else {
            rear.next = node;
            rear = node;
        }
        size++;
    }

    /**
     * 队列头删除元素
     * @return
     */
    private SignLinkedNode delete() {
        if (isEmpty()) {
            throw new RuntimeException("队列空...");
        }
        SignLinkedNode temp = head;
        head = head.next;
        size--;
        return temp;
    }

    /**
     * 遍历队列里面有效数据
     */
    private void show() {
        SignLinkedNode temp = head;
        while (temp != null) {
            System.out.print(temp + ", ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedQuque arrayQueue = new LinkedQuque(5);
        arrayQueue.add(new SignLinkedNode(2));
        arrayQueue.add(new SignLinkedNode(1));
        arrayQueue.add(new SignLinkedNode(5));
        arrayQueue.add(new SignLinkedNode(3));
        arrayQueue.add(new SignLinkedNode(4));
        arrayQueue.add(new SignLinkedNode(7));
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        arrayQueue.add(new SignLinkedNode(8));
        arrayQueue.add(new SignLinkedNode(9));
        arrayQueue.show();
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小绿豆

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值