自己实现队列的基本操作方法(Java实现)

目录

1. void offer (long e) 方法:入队列

2. long poll() 方法:出队列

3. long peek() 方法:查看队首元素

4. boolean isEmpty() 方法:查看队列是否为空

5. int size() 方法:查看队列中元素个数

6. 整体全部代码


    通过链表实现队列的基本操作,同时定义一个内部类 Node 记录每个结点内存储的元素(value)以及下一个元素的结点(next)。并且定义一个头结点(head)和尾结点(last)方便后续的操作。

public class Queue {
    static class Node {
        long value;
        Node next;

        Node(long value) {
            this.value = value;
            this.next = null;
        }
    }

    // 为了方便头删 + 尾插,记录头节点 + 尾结点
    private Node head;
    private Node last;
    private int size;

    public Queue() {
        this.head = this.last = null;
        this.size = 0;
    }
    //后续的方法(还未实现)
    public void offer(long e) {}
    public long poll() {}
    public long peek() {}
    public int size() {}
    public boolean isEmpty() {}
}
        

1. void offer (long e) 方法:入队列

思路:因为队列先进先出,所以可以通过尾插操作来实现。这里需要考虑两种情况:

  • ① size 为 0时:即队列中没有元素时,尾插入一个新元素结点,则头结点和尾结点均指向该新元素结点。
  • ② size != 0 时:即队列中有元素时,last.next 应该指向新元素结点。然后更新 last,即将尾结点指向新元素结点。

代码:

public void offer(long e) {
        Node node = new Node(e);
        if (size == 0 ) {
            this.head = this.last = node;
        } else {
            this.last.next = node;
        }
        this.size++;
    }

2. long poll() 方法:出队列

思路:因为队列先进先出,所以可以通过头删操作实现出队列操作。分三种情况:

  • ① size = 0(队列中没有元素)时:抛出异常。
  • ② size = 1 (队列中只有一个元素)时:记录该唯一结点的 value 值,再将头结点和尾结点同时置空,最后返回提前记录的 value 值即可。
  • ③ size > 1 (队列中至少有两个元素)时:记录头结点元素的 value 值,同时将头结点指向头结点的下一个结点(next),最后返回提前记录的 value 值即可。

总结:观察三种情况,当队列中有元素时,都要进行头结点的更新。区别是尾结点的处理,元素个数为 1 个时,尾结点置空;元素个数大于1时,尾结点不变。

代码:

    public long poll() {
        if (size == 0) {
            throw new RuntimeException("队列中没有元素");
        }
        long e = this.last.value;
        this.head = this.head.next;
        if (this.head == null) {
            //这里的 head 是已经更新后的 head,head == null 说明队列中元素只有一个
            this.last = null;
        }
        this.size--;
        return e;
    }

3. long peek() 方法:查看队首元素

思路:

  • ① size = 0(队列中没有元素)时:抛出异常。
  • ② size != 0(队列中有元素)时:直接返回头结点的元素值。

代码:

    public long peek() {
        if (size == 0) {
            throw new RuntimeException("队列中没有元素");
        }
        return this.head.value;
    }

4. boolean isEmpty() 方法:查看队列是否为空

思路:根据队列元素个数 size 判断即可。

代码:

public boolean isEmpty() {
        return this.size == 0;
    }

5. int size() 方法:查看队列中元素个数

思路:直接返回队列属性 size 值即可。

代码:

 public int size() {
        return this.size;
    }

6. 整体全部代码

public class Queue {
    private class Node{
        long value;
        Node next;

        public Node(long value) {
            this.value = value;
            this.next = null;
        }
    }

    // 为了方便操作(头删和尾插),定义一个头结点和尾结点
    private Node head;
    private Node last;
    private int size;

    //构造方法
    public Queue(){
        this.head = this.last = null;
        this.size = 0;
    }

    //入队列
    public void offer(long e) {
        Node node = new Node(e);
        if (size == 0 ) {
            this.head = this.last = node;
        } else {
            this.last.next = node;
        }
        this.size++;
    }

    //出队列
    public long poll() {
        if (size == 0) {
            throw new RuntimeException("队列中没有元素");
        }
        long e = this.last.value;
        this.head = this.head.next;
        if (this.head == null) {
            //这里的 head 是已经更新后的 head,head == null 说明队列中元素只有一个
            this.last = null;
        }
        this.size--;
        return e;
    }

    //查看队首元素
    public long peek() {
        if (size == 0) {
            throw new RuntimeException("队列中没有元素");
        }
        return this.head.value;
    }

    //查看队列是否为空
    public boolean isEmpty() {
        return this.size == 0;
    }

    //查看队列中的元素个数
    public int size() {
        return this.size;
    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值