LinkedList 源碼淺析

LinkedList類定義
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable{}

可以重點(diǎn)關(guān)注下Deque這個(gè)接口,最后介紹這個(gè)接口。

主要成員變量
 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;
        }
    }

靜態(tài)內(nèi)部類Node是LinkedList的內(nèi)部鏈表節(jié)點(diǎn)結(jié)構(gòu),包含了存儲(chǔ)元素的item,以及前一個(gè)節(jié)點(diǎn)prev,后一個(gè)節(jié)點(diǎn)next。size代表節(jié)點(diǎn)數(shù)量,first是head節(jié)點(diǎn),last是tail節(jié)點(diǎn)。

構(gòu)造方法
 /**
     * 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);
    }

主要有兩個(gè)構(gòu)造方法,第一個(gè)是建立一個(gè)空的list,第二個(gè)是用Collection參數(shù)來建立list。

主要方法
 /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }
/**
     * Links e as last element.
     */
    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++;
    }

添加一個(gè)元素到隊(duì)尾,實(shí)質(zhì)是新產(chǎn)生一個(gè)元素,然后修改其next prev指向,同時(shí)修改first和last指針,最后更新size和modCount

/**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }
/**
     * Links e as first element.
     */
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

這個(gè)方法是在隊(duì)頭添加元素,也是操作鏈表。
其他的poll remove addLast offer 等都是類似的,均是操作鏈表,更新size值和modCount,不再贅述。

Deque接口

先看看接口圖


Deque

Deque繼承了Queue,完整名稱是double-ended-queue,雙端隊(duì)列,隊(duì)頭隊(duì)尾均可以插入和刪除元素,所以這樣可以充當(dāng)隊(duì)列(FIFO)或者Stack(FILO),看看Deque的主要方法。

Deque方法

通過其中的幾個(gè)方法的組合,我們就可以實(shí)現(xiàn)一個(gè)queue或一個(gè)stack。


Deque和queue對(duì)應(yīng)方法

Deque和stack對(duì)應(yīng)方法

需要注意一下方法的返回值。

需要注意的地方

LinkedList實(shí)現(xiàn)了Deque接口,可以當(dāng)做queue或stack來使用,本身不是線程安全的,可以使用Collections.sychronizedList()方法來包裝成線程安全的list。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容