LinkedList 源码实现

Java LinkedList 源码实现

1.链表实现

==双向链表,非同步==

    transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

    private static class Node<E> {
        E item;//节点元素
        Node<E> next;//下一个节点
        Node<E> prev;//上一个节点
        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

==链表的实现方式可以看出,item=null 是ok的, 其可以保存null值==

2.链表结构图

  • A.prev = null;
  • A.next = B;
  • B.prev = A;
  • B.next = C;
  • C.prev = B;
  • C.next = null;
  • A–B–C

image

3.链表操作

3.1 新增

    //链表新增一个元素
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    void linkLast(E e) {
        final Node<E> l = last;
        //e.prev=l,e.next=null
        final Node<E> newNode = new Node<>(l, e, null);
        //lastNode指向新节点,新节点作为最后的节点
        last = newNode;
        /**
         *l==null的话,说明新增的元素是第一个节点,
         *     null=prev.firstNode.next=newNode
         *firstNode=prev.newNode.next=lastNode
         *  newNode=prev.lastNode.next=null
         **/
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }    

3.2 删除

 public boolean remove(Object o) {
        //移除的是null元素的话
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Unlinks non-null node x.
     */
    E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        //待删除的元素是第一个节点
        if (prev == null) {
            first = next;
        } else {
        //待删除元素的前一个节点next索引指向待删除元素的下一个节点
            prev.next = next;
            //待删除元素的前一索引置成null,GC回收
            x.prev = null;
        }
       //待删除的元素是最后一个节点
        if (next == null) {
            last = prev;
        } else {
        //待删除元素的下一个元素的prev索引指向待删除元素的前一个节点
            next.prev = prev;
            //元素后置索引置成null,GC
            x.next = null;
        }
        //元素值置为null,GC
        x.item = null;
        size--;
        modCount++;
        return element;
    }

==由实现可以看出其移除的是第一个匹配值的节点==

ArrayList 与LinkedList的区别

  1. 都是非同步的
  2. ArrayList基于数组实现(get,set,随机访问,效率高),LinkedList基于链表实现(add,remove效率高)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值