1. 程式人生 > >資料結構(二)LinkedList原始碼分析

資料結構(二)LinkedList原始碼分析

一、基本概念

1、關係圖:
這裡寫圖片描述

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

實現了List和Deque,內部是一個雙向連結串列
2、圖解:
這裡寫圖片描述
連結串列資料單元分為資料域和指標域,儲存資料和指向下一個元素的位置,雙向連結串列是指某元素的next指向下個元素, preview指向上個元素。

特點:

  • 雙向連結串列的實現
  • 存放地址的空間不需要連續
  • 元素儲存了上一個元素和下一個元素的引用
  • 首元素的preview和尾元素的next置null
  • 元素有序,輸出順序和輸入順序一致

二、建構函式和成員變數:

1、成員變數:

// 記錄當前連結串列的長度
transient int size = 0;
// 第一個節點
transient Node<E> first;
// 最後一個節點
transient Node<E> last;

2、Node:

包括了當前資料,上個元素的引用、下個元素的引用

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; } }

3、建構函式:

public LinkedList() {
}

public LinkedList
(Collection<? extends E> c) { this(); addAll(c); }

新增元素

public boolean addAll(Collection<? extends E> c) {
    return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
    checkPositionIndex(index);
    //轉為陣列
    Object[] a = c.toArray();
    int numNew = a.length;
    if (numNew == 0)
        return false;

    Node<E> pred, succ;
    if (index == size) {
        succ = null;
        pred = last;
    } else {
        succ = node(index);
        pred = succ.prev;
    }
    //將每個元素轉為Node
    for (Object o : a) {
        @SuppressWarnings("unchecked") E e = (E) o;
        Node<E> newNode = new Node<>(pred, e, null);  
        if (pred == null)
            //給首個元素賦值
            first = newNode;
        else
            //next指向當前元素
            pred.next = newNode;
        pred = newNode;
    }

    if (succ == null) {
        last = pred;
    } else {
        pred.next = succ;
        succ.prev = pred;
    }
    //size更新
    size += numNew;
    modCount++;
    return true;
}

三、新增元素:

1、常用方法

//隊首新增元素
public void addFirst(E e) {
    linkFirst(e);
}
//隊尾新增元素
public void addLast(E e) {
    linkLast(e);
}
//新增到隊尾
public boolean add(E e) {
    linkLast(e);
    return true;
}
//新增到某個位置
public void add(int index, E element) {
    checkPositionIndex(index);

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

具體實現都是通過linkFirst、linkLast、linkBefore這三個方法。

2、在隊首新增元素

   private void linkFirst(E e) {
        final Node<E> f = first;
        //建立一個新的節點,next指向之前的first節點
        final Node<E> newNode = new Node<>(null, e, f);
        //將first節點指向新建的節點
        first = newNode;
        if (f == null)
            last = newNode;
        else
            //把原連結串列的preview指向現在的first節點
            f.prev = newNode;
        size++;
        modCount++;
    }

3、在某個節點前新增元素:

void linkBefore(E e, Node<E> succ) {
    // 記錄某節點的preview的指向
    final Node<E> pred = succ.prev;
    // 建立需要被新增的元素
    final Node<E> newNode = new Node<>(pred, e, succ);
    // 某節點的preview指向新元素
    succ.prev = newNode;
    if (pred == null)
        first = newNode;
    else
    //之前的next節點指向被新增的元素
        pred.next = newNode;
    size++;
    modCount++;
}

四、刪除元素

1、常用方法:

//移除某位置元素
public E remove(int index) {
    checkElementIndex(index);
    return unlink(node(index));
}
//移除隊首元素
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 removeLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return unlinkLast(l);
}

具體實現通過unlinkFirst、unlink、unlinkLast三個方法。

2、刪除首位的元素

private E unlinkFirst(Node<E> f) {

        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;
}

3、刪除某元素

E unlink(Node<E> x) {
    final E element = x.item;
    //獲取當前元素的next和prview元素
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;
    //上個元素的next指向下個元素
    if (prev == null) {
        first = next;
    } else {
        prev.next = next;
        x.prev = null;
    }
    //下一個元素的preview指向上個元素
    if (next == null) {
        last = prev;
    } else {
        next.prev = prev;
        x.next = null;
    }

    x.item = null;
    size--;
    modCount++;
    return element;
}

五、獲取元素:

1、常用方法:

//獲取隊首元素
public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}
//獲取隊尾元素
public E getLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return l.item;
}

2、獲取某個位置的元素

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;
    }
}

3、總結:

  • 在隊尾和隊首插入和刪除資料非常方便
  • 在中間位置根據索引去增刪或者查詢都是需要進行折半遍歷,效率不高。