集合—LinkedList源码

2-3 LinkedList(2021-11-4)

1 说明

  • LinkedList底层实现了双向链表双向队列特点。
  • 可以添加任何元素(可重复,可null)。
  • 线程不安全,没有实现同步。
  • 属性:
    • size(链表中元素)
    • first(指向 维护的链表(数据)的第一个元素)
    • last(指向 维护的链表 最后)
  • 每个节点(Node对象),里面又维护了prev,next。

2 源码

  • 无参构造:
public LinkedList() { //size = 0
}
  • add:
public boolean add(E e) {
    linkLast(e){ //把对象挂到尾巴上!
        final Node<E> l = last; // l ——> null
        final Node<E> newNode = new Node<>(l, e, null); // first——>null , item , null
        last = newNode; // last ——> newNode
        if (l == null)
            first = newNode; // first——>newNode
        else
            l.next = newNode;
        size++; //长度+1
        modCount++;
	};]
    return true;
}

newNode(pre) ——> l
last ——> newNode
l.next ——> newNode

在这里插入图片描述

remove():这里默认删除的是第一个结点。

public E remove() {
    return removeFirst(){//删除第一个结点
		final Node<E> f = first; //指向第一个结点
        if (f == null) //防止空
            throw new NoSuchElementException();
        return unlinkFirst(f){   //真正拿掉第一个结点的代码!!!!!!!!!!!!
            // assert f == first && f != null;
            final E element = f.item; //取出内容
            final Node<E> next = f.next; //指向第二个结点
            f.item = null; //置空
            f.next = null; // help GC
            first = next; //指向第二个结点
            if (next == null)
                last = null;
            else
                next.prev = null;
            size--;
            modCount++;
            return element;
		};
    };
}

Files\1440590839\FileRecv\MobileFile\IMG_0779(20211104-195406).PNG" alt="IMG_0779(20211104-195406)" style="zoom:80%;" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值