队列 -- JAVA

一. 队列

  1. 队列是只允许在一端进行插入数据操作, 在另一端进行删除数据操作的特殊线性表. 进行插入操作的一端称为队尾, 进行删除操作的一端称为队头.
    在这里插入图片描述
  2. 用链表实现队列(较常用):
public class MyQueue {
   
    // 链表尾部作为队尾(方便插入元素), 链表头部作为队首(方便删除元素)
    // 为了更方便的实现尾插操作, 多搞一个引用指向链表的尾部
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    private Node head = new Node(-1); // 傀儡节点
    private Node tail = head;

    // private int size; 
    // 队列中的元素个数

    // 入队列(链表尾插)
    public void offer(int value) {
        Node newNode = new Node(value);
        tail.next = newNode;
        // 插入完毕之后别忘了更新 tail 的指向. 要让 tail 始终指向链表的尾部
        tail = tail.next;
    }

    // 出队列(链表头删)
    public Integer poll() {
        if (head.next == null) {
            // 队列为空, 出队列失败
            return null;
        }
        Node toDelete = head.next;
        head.next = toDelete.next;
        // 不要忘了更新tail
        if (head.next == null) {
            // 此时队列已经为 空 了.
            // 让 tail 能够再指回傀儡节点
            tail = head;
        }
        return toDelete.val;
    }

    // 取队首元素
    public Integer peek() {
        if (head.next == null) {
            // 空队列
            return null;
        }
        return head.next.val;
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        while (true) {
            Integer cur = queue.poll();
            if (cur == null) {
                break;
            }
            System.out.println(cur);
        }
    }
}
  1. 用顺序表实现队列:
public class MyQueue {
    private int[] array = new int[100];
    // [head, tail) 初始情况下队列中应该是没有元素的.
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    // 如果插入失败, 返回 false
    // 如果插入成功, 返回 true
    public boolean offer(int value) {
        if (size == array.length) {
            return false;
        }
        array[tail] = value;
        tail++;
        // 下面的两种写法效果是一样的
//        if (tail >= array.length) {
//            tail = 0;
//        }
        tail = tail % array.length;
        size++;
        return true;
    }

    public Integer poll() {
        if (size == 0) {
            // 队列为空, 出队列失败
            return null;
        }
        // 队列非空, 返回 head 位置的元素, 同时 head++ 删除该元素
        int ret = array[head];
        head++;
        if (head >= array.length) {
            head = 0;
        }
        size--;
        return ret;
    }

    public Integer peek() {
        if (size == 0) {
            return null;
        }
        return array[head];
    }

    public static void main(String[] args) {
        MyQueue2 myQueue2 = new MyQueue2();
        myQueue2.offer(1);
        myQueue2.offer(2);
        myQueue2.offer(3);
        myQueue2.offer(4);
        while (true) {
            Integer cur = myQueue2.poll();
            if (cur == null) {
                break;
            }
            System.out.println(cur);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值