单链表实现 简单队列

package linkede;

/** 队列 (单链表)
 * @author codelmh
 * @data 2021/12/9
 */
public class MyQueue<T> {
    private Node head; // 指向 头结点
    private Node tail;//指向尾结点
    private Integer size; // 大小

    public T peek(){
        if (head == null){
            return null;
        }
        return head.value;
    }
    /**
     * 弹出一个节点
     */
    public T poll(){
        if (head == null){
            return null;
        }
        Node cur = head;
        head = cur.next;
        size --;
        return cur.value;
    }

    /**
     * 尾插
     * @param value
     */
    public void offer(T value){
        //新节点
        Node newNode = new Node(value);
        // head为空说明队列为空
        if ( this.head == null){
            this.head = newNode;
            this.tail = newNode;
            return;
        }
        this.tail.next = newNode;
        this.tail = newNode;
    }

    /**
     * 头插法
     * @param value
     */
    public void addHead(T value){
        //新节点
        Node newNode = new Node(value);
        // head为空说明队列为空
        if ( this.head == null){
            this.head = newNode;
            this.tail = newNode;
            return;
        }
        // 新节点的netx域 指向 头指针
        newNode.next = head;
        // 头指针指向 newNode
        head = newNode;
        size ++;
    }

    public MyQueue() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    private class Node {
        T value; //数据
        Node next; //指针

        // 节点初始化
        public Node(T value) {
            this.next = null;
            this.value = value;
        }
    }
}

本人小白一枚,若有错误请指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值