Java LinkedList
通過雙向鏈表(Doubly-linked
)實現,實現了List
和Deque
接口,所以LinkedList
可以作為List
的使用,也可以作為Stack
和Queue
來使用。
作為List使用
結構
LinkedList中維護兩個指向鏈表第一個節點和最后一個節點的指針。Node
是一個私有內部類,Node
類中存有值item
,和兩個指向上一結點和下一節點的指針。
整個LinkedList
是由一個個Node
節點組成的,為了維護每個Node
的上下節點信息,鏈表需要使用更多的空間。
transient Node<E> first;
transient Node<E> last;
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
...
}
我們通過以下小例子來看一下LinkedList的存儲結構。
List<String> list = new LinkedList<>();
list.add("語文: 1");
list.add("數學: 2");
list.add("英語: 3");
結構圖示如下

add方法
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);// l是prev節點,e是item值,next節點為null
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
add方法挺簡單的,就是在鏈表尾部添加一個新的Node,也就是調用linkLast(e)
方法。不過有一點要注意的是,當l==null
,
也就是目前鏈表只有一個節點,所以first
和last
指向同一個節點。
get方法
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;
}
}
get方法中,調用了Node方法。Node方法會判斷index是在前半區還是后半區,如果是在前半區,就從first開始往后搜索,
如果是在后半區,就從last開始往后搜索。這樣使原本查找性能由O(n)
變為O(n/2)
。
remove方法
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
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;
}
add方法是linkLast
,而remove方法是unlinkFirst
。這里也要判斷一下特殊情況,當next==null
的時候,就是LinkedList中沒有節點時,
last要設置為null
。
作為Queue使用
LinkedList實現了Deque接口,Deque接口又繼承了Queue接口,所以LinkedList也可以作為Queue使用。
下面通過一個小例子來展示一下作為Queue使用的LinkedList。
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < 5; i++) {
queue.add(i);
}
while (!queue.isEmpty()) {
Integer i = queue.poll();
System.out.printf("%d ", i);
}
}
我們使用Queue的add
方法來向隊列添加元素,使用poll
方法來獲取元素。Queue的remove
方法也可以用來獲取元素,
但是當列表為空時,remove
方法會拋出異常,而poll
方法會返回null。
add方法剛才已經介紹了,來看一下poll
方法。
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
好吧,poll方法只是判斷f是否為null,如果不是就調用unlinkFirst
方法,這個方法剛才我們也介紹過了。
作為Stack(Deque)使用
Java中其實是有一個Stack
類的,但是由于某些原因,Java推薦我們使用Deque
來代替Stack
。
A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.
而LinkedList實現了Deque
接口,所以也可以作為Stack
使用。來看一個小栗子:
public static void main(String[] args) {
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < 5; i++) {
stack.push(i);
}
while (!stack.isEmpty()) {
Integer i = stack.pop();
System.out.printf("%d ", i);
}
}
我們使用Deque的push
方法來向棧中添加元素,使用pop
方法來獲取元素。
public void push(E e) {
addFirst(e);
}
public E pop() {
return removeFirst();
}
pop方法中調用了removeFirst
方法,剛才我們也介紹過了。push
方法調用了addFirst
方法,
和linkLast
類似,只不過是把添加到鏈表尾部改為添加到頭部,就不多贅述了。
圖和部分代碼摘自Java LinkedList工作原理及實現