读懂LinkedList这一篇就够了

本文全部代码来自JDK1.8java

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

简介

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属性,重写了序列化函数,记录这个链表属性的状态bash

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)也是没有返回值的。ui

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是已经越界。this

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

    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)为例spa

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

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

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

四、链表大小增长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)为例

一、先记录如今的first元素,和如今first的next元素记做next(将被做为first)

二、由于first.prev就是null,只用对f.next置null就至关于开头例子中松开与后面小朋友的手同样,f.item 也要置null,更好的垃圾回收

三、删除以后,next成为链表的first元素//思考是否是能够结束了?

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

五、链表大小减小1,改动次数增长1

六、返回删除的元素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说码

相关文章
相关标签/搜索