概述
LinkedList是List的另一種實現,他的底層是基於雙向鏈表實現的,因此它具有插入刪除快,查詢慢的特點,此外,對雙向鏈表操作還可以實現隊列和棧的功能。
基本數據
結點類結構
//結點內部類
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;
}
}
成員變量和構造方法
//集合元素個數
transient int size = 0;
//頭結點引用
transient Node<E> first;
//尾節點引用
transient Node<E> last;
//無參構造器
public LinkedList() {}
//傳入外部集合的構造器
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
LinkedList持有頭結點和尾結點的引用,他有兩個構造器,一個是無參,一個是傳入外部集合的構造器。
增刪改
//增(添加)
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));
}
}
//刪(給定下標)
public E remove(int index) {
//檢查下標是否合法
checkElementIndex(index);
return unlink(node(index));
}
//刪(給定元素)
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
//遍歷鏈表
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
//找到了就刪除
unlink(x);
return true;
}
}
}
return false;
}
//改
public E set(int index, E element) {
//檢查下標是否合法
checkElementIndex(index);
//獲取指定下標的結點引用
Node<E> x = node(index);
//獲取指定下標結點的值
E oldVal = x.item;
//將結點元素設置爲新的值
x.item = element;
//返回之前的值
return oldVal;
}
//查
public E get(int index) {
//檢查下標是否合法
checkElementIndex(index);
//返回指定下標的結點的值
return node(index).item;
}
- 添加元素的方法只要調用linkLast和linkBefore兩個方法,linkLast方法是在鏈表後面鏈接一個元素,linkBefore方法是在鏈表中間插入一個元素。
- 刪除方法是通過unlink方法將某個元素移除。
看一看刪除的核心方法
//鏈接到指定結點之前
void linkBefore(E e, Node<E> succ) {
//獲取給定結點的上一個結點引用
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++;
}
//卸載指定結點
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) {
//將頭結點引用指向給定結點的下一個結點
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;
}
- linkedBefore是中間插入
- unlink 刪除指定節點
插入刪除的複雜度都是O(1),而對於鏈表的查找和修改操作都需要遍歷整個鏈表進行。
//根據指定位置獲取結點
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;
}
}
- 對index先做了一個判斷,然後從左邊/右邊開始查找。時間複雜度是O(n/2)。
單向隊列、雙向隊列、棧
單向隊列
//獲取頭結點
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
//獲取頭結點
public E element() {
return getFirst();
}
//彈出頭結點
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
//移除頭結點
public E remove() {
return removeFirst();
}
//在隊列尾部添加結點
public boolean offer(E e) {
return add(e);
}
雙向隊列
//在頭部添加
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
//在尾部添加
public boolean offerLast(E e) {
addLast(e);
return true;
}
//獲取頭結點
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
//獲取尾結點
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
棧
//入棧
public void push(E e) {
addFirst(e);
}
//出棧
public E pop() {
return removeFirst();
}
- 不管是隊列還是棧,其都是對鏈表的頭尾結點進行操作。
總結
- LinkedList是基於雙向鏈表實現的,增刪改查、隊列和棧,都可通過操作結點實現
- LinkedList因爲基於鏈表操作,集合的容量隨元素加入自動增加,刪除而自動縮小
- LinkedList對方法沒有進行同步,因此他不是線程安全的