LinkedList源碼詳解

LinkList概述

LinkedList 是 List 接口鏈表的實現。基於雙向鏈表實現的方式使得 LinkedList 在插入和刪除時更優於 ArrayList,而隨機訪問則比 ArrayList 遜色些。
但也是線程不安全

這裏寫圖片描述

LinkList構造方法

    //構造一個空列表
    public LinkedList() {
    }
    //構造一個包含指定 collection 中的元素的列表,這些元素按其 collection 的迭代器返回的順序排列。
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    //將此列表中指定位置的元素替換爲指定的元素。 
    public E get(int index) {
        //檢查索引是否合法,檢查規則:判斷傳入索引是否大於0和傳入索引是否比當前列表的數量小
        checkElementIndex(index);
        //
        return node(index).item;
    }
    //查找結點,採用的是折半查找
    Node<E> node(int index) {
        // assert isElementIndex(index);
        //(size>>1)相當於除以2,
        //如果傳入索引小於(size/2),那麼從列表頭開始查找
        if (index < (size >> 1)) {
            //將列表頭的結點賦值給x
            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;
        }
    }

add方法

boolean add(E e),將指定元素添加到列表的結尾
public boolean add(E e) {
        linkLast(e);//將元素e鏈接到列表末尾
        return true;
    }
void linkLast(E e) {
        //last:最後一個結點,如果是第一次添加,last的初始值爲NULL,將last賦值給l的
        final Node<E> l = last;
        //創建一個新的結點
        final Node<E> newNode = new Node<>(l, e, null);
        //將新的結點賦值給last
        last = newNode;
        //l==null成立說明:當前列表沒有結點,添加到鏈表的第一個位置,鏈表頭
        if (l == null)
            first = newNode;
        //不成立則說明:將新結點的引用賦值給上一個結點的後續元素
        else
            l.next = newNode;
        size++;
        modCount++;
    }   
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;
        }
    }
void add(int index, E element)

指定的位置插入指定的元素。移動當前在該位置處的元素(如果有),所有後續元素都向右移(在其索引中添加 1)。

    public void add(int index, E element) {
        checkPositionIndex(index);
        //如果傳入索引等於列表大小
        if (index == size)
            //往列表尾添加新的結點,跟add()方法一樣
            linkLast(element);
        else
            //先找到插入索引的元素,然後將那個元素向後移動,騰出位置,給新的結點存儲
            linkBefore(element, node(index));
    }
    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++;
    }

刪除指定索引元素

    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }
    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;
    }

迭代器

因爲Linkedlist多重繼承了多個類,會先調用父類的方法,

public ListIterator<E> listIterator() {
        //這裏傳遞了一個0值
        return listIterator(0);
    }

真正的有作用的方法是

    public ListIterator<E> listIterator(int index) {
        //判斷index傳入的值是否大於等於0與是否小於元素的大小
        checkPositionIndex(index);
        return new ListItr(index);
    }

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

    ListItr(int index) {
        //取出鏈表頭賦值給next
        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 boolean hasPrevious() {
        return nextIndex > 0;
    }

    public E previous() {
        checkForComodification();
        if (!hasPrevious())
            throw new NoSuchElementException();

        lastReturned = next = (next == null) ? last : next.prev;
        nextIndex--;
        return lastReturned.item;
    }

    public int nextIndex() {
        return nextIndex;
    }

    public int previousIndex() {
        return nextIndex - 1;
    }

    public void remove() {
        checkForComodification();
        if (lastReturned == null)
            throw new IllegalStateException();

        Node<E> lastNext = lastReturned.next;
        unlink(lastReturned);
        if (next == lastReturned)
            next = lastNext;
        else
            nextIndex--;
        lastReturned = null;
        expectedModCount++;
    }

    //替換元素
    public void set(E e) {
        if (lastReturned == null)
            throw new IllegalStateException();
        checkForComodification();
        lastReturned.item = e;
    }
    //添加到當前索引的元素
    public void add(E e) {
        checkForComodification();
        lastReturned = null;
        if (next == null)
            linkLast(e);
        else
            linkBefore(e, next);
        nextIndex++;
        expectedModCount++;
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章