開篇
?LinkedList基于鏈表實現(xiàn)严沥,在List中間進行插入和刪除的代價較低,提供了優(yōu)化的順序訪問。LinkedList在隨機訪問方面相對比較慢,但是它的特性集較ArrayList更大讥珍。
?LinkedList的實現(xiàn)是一個雙向鏈表,LinkedList存儲的Node節(jié)點包含指向前置后置節(jié)點的指針窄瘟。
LinkedList類圖
LinkedList類圖
LinkedList的數(shù)據(jù)存儲結構圖
LinkedList類定義
?LinkedList的類定義中包含first節(jié)點和last節(jié)點衷佃,通過first節(jié)點(指向頭節(jié)點)和last節(jié)點(指向尾節(jié)點)將串聯(lián)所有的list中的節(jié)點,看下Node的定義就知道了蹄葱。
? Node的prev和next節(jié)點分別指向前后節(jié)點氏义。
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;
// 指向LinkedList的第一個節(jié)點
transient Node<E> first;
// 指向LinkedList的最后一個節(jié)點
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;
}
}
}
LinkedList構造函數(shù)
?LinkedList的構造函數(shù)非常簡單,關鍵是看下參數(shù)為Collection的構造函數(shù)图云,在該構造函數(shù)當中通過addAll()方法將元素通過尾插入法添加到LinkedList當中惯悠。allAll參數(shù)的index標記從哪個位置開始插入。
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) {
// 確定是否超過index的下標
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
// 確定插入位置的前后節(jié)點位置竣况,pred是前置節(jié)點克婶,succ是后置節(jié)點
Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
// 直接采用鏈表插入法插入即可
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
LinkedList常用操作
LinkedList的add方法
?LinkedList的add()方法其實非常簡單,就是在LinkedList的尾部進行插入丹泉,然后更新last節(jié)點就可以了情萤。
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);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
// 設計巧妙,力求最少時間定為索引位置
Node<E> node(int index) {
// assert isElementIndex(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;
}
}
// 在合適的節(jié)點之前插入
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
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++;
}
LinkedList的remove方法
?LinkedList的remove()的方法也非常簡單摹恨,通過移除頭部節(jié)點即可筋岛,然后將first節(jié)點后移即可。
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;
}
LinkedList的indexOf方法
?LinkedList的indexOf()方法主要從first到last進行遍歷依次比較即可晒哄。
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
LinkedList迭代器
?LinkedList的迭代器主要分為兩個:
- iterator主要是在AbstractList類中定義泉蝌,通過java的多態(tài)性調用LinkedList的size()方法和get()方法實現(xiàn)索引的比較和數(shù)據(jù)的獲取等。
- listIterator在LinkedList類中實現(xiàn)揩晴,通過index指定迭代器開始遍歷的位置,通過前后指針進行next移動贪磺,通過index和size比較是否遍歷完成硫兰。
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
// 調用LinkedList的size()方法
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
// get()方法調用的是LinkedList的方法
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
}
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
public boolean hasNext() {
return nextIndex < size;
}
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
}