读懂LinkedList这一篇就够了

本文所有代码来自JDK1.8

想象幼儿园小朋友放学回家,老师要求小朋友们排成一字队,前后小朋友都手牵着手,举个栗子小明是排在前面第一位同学,他的右手牵着后面小红的左手,小红的右手牵着后面小强的左手,以此类推,直到所有大家都手牵着手,开开心心的放学回家~ 由于小冯刚刚拉肚子没有在队伍中,他现在想加入到队伍中而且想站在小红的后面,他需要先让小红和小强的手松开,然后左手牵住小红的右手,右手牵住小强的左手,这样就加入到队伍中了。 其实这种结构就像LinkedList

简介

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
复制代码

LinkedList继承AbstractSequentialList类,实现了List, Deque(双端队列,具有队列和栈性质的一种数据结构), Cloneable, java.io.Serializable接口。

如果可以用简短一段话概括的话: 底层结构是Node构成的双向链表,每个Node具有前后两个指针,不要求存储空间连续;适合插入与删除,是有序的,不是线程安全的,允许null元素,允许重复元素

注意 remove()方法,其实是删除链表第一个元素,后面详细介绍

源码分析

成员属性及构造函数

    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;

    /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }

    /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param  c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
复制代码

size为此列表中元素个数,初始化为0,first始终指向列表中第一个元素,last始终指向列表最后一个元素,这三个成员都具有transient属性,重写了序列化函数,记录这个链表属性的状态

LinkedList()此无参构造函数中没有都没有做,对比在ArrayList中的无参构造函数创建一个为默认大小为10的空列表。

LinkedList(Collection<? extends E> c)构建一个包含指定集合的所有元素的列表,元素顺序按照集合迭代器返回的顺序

存储单元

    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;
        }
    }
复制代码

常用函数及底层辅助函数

   public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

    Node<E> node(int index) {
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

-----------辅助函数------------

    private void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }
复制代码

这个很简单,通过index >= 0 && index < size判断是否在范围内,若不在抛出下标越界异常;否则值得注意,node(int index)通过与size/2大小判断索引是在链表前半段还是后半段,相比于每次都从first向last遍历,这种方式可减少遍历次数。

    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    public boolean offer(E e) {
        return add(e);
    }

    public void addLast(E e) {
        linkLast(e);
    }

    public boolean offerLast(E e) {
        addLast(e);
        return true;
    }
----------------------------------
    public void push(E e) {
        addFirst(e);
    }

    public void addFirst(E e) {
        linkFirst(e);
    }

    public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }

复制代码

由于实现了Deque接口,方法较多,但其实调用的底层代码是一样的,一般还是使用add较多。值得注意的是Deque中addLast(E e)只是在链表最后插入元素但是没有返回值的,同理addFirst(E e)也是没有返回值的。

    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

    private void checkPositionIndex(int index) {
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }
复制代码

值得注意isPositionIndex()和之前isElementIndex()有小小区别,在此index <= size说明可以在最后插入元素,但是在get()中index为size是已经越界。


-----------辅助函数------------

    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        final Node<E> newNode = new Node<>(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }
复制代码

前一个方法是在链表后面添加节点,后一个方法是在链表前面添加节点,以讲linkLast(E e)为例

1、链表中最后一个元素last用l表示

2、构造new Node<>(l, e, null)新节点,前向指针指向最后一个元素,后向指针指向null

3、last = newNode表示将新的节点插入到最后面,判断此链表是否为空,若是则链表的first指向新加入的节点;若不是就将之前为last的节点后向指针指向新插入的节点。

4、链表大小增加1,改动次数增加1

    public E remove() {
        return removeFirst();
    }

    public E pop() {
        return removeFirst();
    }

    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }
复制代码

于实现了Deque接口,方法较多,但其实调用的底层代码是一样的,一般还是使用remove较多。第一个值得注意remove()pop()对空链表情况会抛出NoSuchElementException,同样情况在poll()中会返回null。第二个值得注意的是remove()删除的是第一个元素,而不是最后的元素,若想删除最后的元素,应该使用removeLast()


    private E unlinkFirst(Node<E> 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;
    }


复制代码

unlinkFirst(Node<E> f)方法是在链表删除first元素,unlinkLast(Node f)方法是在链表删除last元素,原理类似,详细介绍unlinkFirst(Node<E> f)为例

1、先记录现在的first元素,和现在first的next元素记作next(将被作为first)

2、因为first.prev就是null,只用对f.next置null就相当于开头例子中松开与后面小朋友的手一样,f.item 也要置null,更好的垃圾回收

3、删除之后,next成为链表的first元素//思考是不是可以结束了?

4、判断next是否为空,若是,last值为null( first = next,first也为null);若不是则next.prev置null,成为真正意义上的first

5、链表大小减少1,改动次数增加1

6、返回删除的元素item

--------------------------------
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }

    public boolean remove(Object o) {
        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;
    }

-----------辅助函数------------

    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 {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }
复制代码

remove(Object o)删除链表中的元素,从first向last遍历,直到找到第一个出现的对应元素,然后删除,返回true;若找不到则返回false。例子中假设o叫做小欧,让小欧前面小朋友先通过小欧牵住小欧后面小朋友的手(同理对后面小朋友来说也就牵住了前面小朋友的手),然后小欧就从队伍中分离了,也就删除了元素o。

    public void clear() {
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }
复制代码

清除所有的元素,item、prev、next及first、last全部置为null,size置为0,改动次数增加1。清除节点之间的所有链接是“不必要的”,但是可帮助一代GC确保释放内存。

LinkedList遍历比较

第一个问题普通循环方法慢,而其他遍历方法更快?迭代器内部结构便知

    private class ListItr implements ListIterator<E> {
        private Node<E> lastReturned;
        private Node<E> next;
        private int nextIndex;
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index);
            nextIndex = index;
        }

        public boolean hasNext() {
            return nextIndex < size;
        }

        public E next() {
            checkForComodification();
            if (!hasNext())
                throw new NoSuchElementException();

            lastReturned = next;
            next = next.next;
            nextIndex++;
            return lastReturned.item;
        }
        ......
}
复制代码

通过跟踪源码,我们知道后三种方式都是通过以下执行的,所以时间花费相差不大

    public ListIterator<E> listIterator() {
        return listIterator(0);
    }
复制代码

迭代器和普通循环方法对比起来少了一个list.get(i),LinkedList中调用了一次get(i),就从我们知道这个时间复杂度应该是O(n),再嵌套一个for循环是O(n^2);迭代器其实每次没从first或last重新开始遍历,记录当前的位置,iterator中因为next的存在所以循环下来时间复杂度是O(n),因此差距就在这里。

第二个问题ArrayList的普通循环方法快?

上篇文章读懂ArrayList这一篇就够了有讲,ArrayList基于索引结构,元素是在堆空间连续存储的地址,查找元素不用遍历,所花费时间很少。而LinkedList中元素是在堆空间中不连续,通过prev、next指针相连的。

持续输出高质量干货,欢迎关注公众号:Duffy说码

转载于:https://juejin.im/post/5c95d7e5e51d455a3053d002

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值