LinkedList的原理介紹

一、LinkedList的概述

  1. LinkedList是雙向鏈表實現的List
  2. LinkedList是非線程安全的
  3. LinkedList元素允許爲null,允許重複元素
  4. LinkedList是基於鏈表實現的,因此插入刪除效率高,查找效率低(雖然有一個加速動作)
  5. LinkedList是基於鏈表實現的,因此不存在容量不足的問題,所以沒有擴容的方法
  6. LinkedList還實現了棧和隊列的操作方法,因此也可以作爲棧、隊列和雙端隊列來使用

二、LinkedList的分析

LinkedList的存儲結構

LinkedList是由雙鏈表的數據結構組成的

public class LinkedList{
// 元素個數
transient int size = 0;
/**
指向第一個節點的指針
不變性:
1. 如果first = null,則last=null
2. 如果first.prev == null,則first.item != null
*/
transient Node<E> first;
/**
指向最後一個節點的指針
不變性:
1. 如果first = null,則last = null
2. 如果last.next == null,則last.item != null
*/
transient Node<E> last;
private static class Node<E> { E item;
Node<E> next; // 下一個Node的引用
Node<E> prev; // 上一個Node的引用
Node(Node<E> prev, E element, Node<E> next) { this.item = element;
this.next = next; this.prev = prev;
}
}
/**
創建一個空list
* */
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) { this();
addAll(c);
}
}

 

添加元素

從頭部添加

// 從頭插入
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) { final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f); first = newNode;
if (f == null) // 當前List中沒有元素,size=0
last = newNode; else
f.prev = newNode; size++;
modCount++;
}

從尾部添加

public boolean add(E e) { linkLast(e);
return true;
}
public void addLast(E e) { linkLast(e);
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null); last = newNode;
if (l == null)// 當前List中沒有元素,size=0
first = newNode; else
l.next = newNode; size++;
modCount++;
}

刪除節點

從頭部刪除

// 移除首節點,並返回該節點的元素值
public E remove() { return removeFirst();
}
public E removeFirst() { final Node<E> f = first; if (f == null)
throw new NoSuchElementException(); return unlinkFirst(f);
}
// 刪除首節點f
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) // size=1 last = null;
else
next.prev = null; size--;
modCount++; return element;
}

從尾部移除

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

private E unlinkLast(Node<E> l) { final E element = l.item; final Node<E> prev = l.prev; l.item = null;
l.prev = null; // help GC last = prev;
if (prev == null) // size=1 first = null;
else
prev.next = null; size--;
modCount++; return element;
}

根據索引移除

public E remove(int index) { checkElementIndex(index);// 檢查索引index範圍return unlink(node(index));
}
E unlink(Node<E> x) {
final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev;
if (prev == null) {// x爲首節點first = next;
} else {
prev.next = next; x.prev = null;
}
if (next == null) {// x爲尾節點last = prev;
} else {
next.prev = prev; x.next = null;
}
x.item = null; size--; modCount++; return element;
}

獲取節點數據

獲取頭部數據

// 獲取首節點的數據
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;
}

根據索引獲取節點數據

// 獲取索引對應節點的數據
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;
}
}

三、總結:LinkedList和ArrayList的比較

順序插入速度ArrayList會比較快,因爲ArrayList是基於數組實現的,數組是事先new好的,只要往指定位置 塞一個數據就好了

LinkedList則不同,每次順序插入的時候LinkedList將new一個對象出來,如果對象比較大,那麼new的時間 勢必會長一點,再加上一些引用賦值的操作,所以順序插入LinkedList必然慢於ArrayList

ArrayList的遍歷效率會比LinkedList的遍歷效率高一些

LinkedList做插入、刪除的時候,慢在尋址,快在只需要改變前後Node的引用地址

ArrayList做插入、刪除的時候,慢在數組元素的批量copy,快在尋址

如果確定插入、刪除的元素是在前半段,那麼就使用LinkedList

如果確定插入、刪除的元素在比較靠後的位置,那麼可以考慮使用ArrayList

如果不能確定插入、刪除是在哪兒呢?建議使用LinkedList,

一來LinkedList整體插入、刪除的執行效率比較穩定,沒有ArrayList這種越往後越快的情況

二來插入元素的時候,弄得不好ArrayList就要進行一次擴容,而ArrayList底層數組擴容是一個既消 耗時間又消耗空間的操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章