LinkedList 是一個鏈?zhǔn)降臄?shù)據(jù)存儲結(jié)構(gòu), 不是線程安全的 , 不支持隨機(jī)訪問,可序列化胰舆,
實(shí)現(xiàn)的接口
從接口上看骚露,并不支持隨機(jī)訪問的功能,實(shí)際上也沒有插入的功能
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
構(gòu)造函數(shù)
//鏈?zhǔn)酱鎯Φ暮锰幘驮谟谒嘉粒挥迷跇?gòu)造時荸百,初始化容量,
public LinkedList() {
}
//構(gòu)造時滨攻,初始化數(shù)據(jù)
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
Node
//節(jié)點(diǎn)結(jié)構(gòu)够话,雙鏈結(jié)構(gòu)
private static class Node<E> {
E item;
Node<E> next; //上一個節(jié)點(diǎn)
Node<E> prev; //下一個節(jié)點(diǎn)
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
數(shù)據(jù)鏈的操作
//這是一個插入元素的操作,相比ArrayList的拷貝操作光绕,性能高很多
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++;
}
//這種查找優(yōu)化方式女嘲,在鏈?zhǔn)酱鎯Y(jié)構(gòu)中很常見,先判斷節(jié)點(diǎn)的大概位置诞帐,再決定遍歷方向
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é)點(diǎn)
E unlink(Node<E> x) {
// assert x != null;
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;
}
add
public boolean addAll(int index, Collection<? extends E> c) {
//檢查下標(biāo)參數(shù)
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;
}
//重點(diǎn)在這里欣尼,創(chuàng)建鏈結(jié)構(gòu)
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;
//記錄修改次數(shù)
modCount++;
return true;
}
remove
//這兩種方式,通過下標(biāo)移除元素的方式是性能比較高停蕉。通過元素引用移除愕鼓,是直接暴力的便利搞定的
public boolean remove(Object o) {
if (o == null) {
//這里訪問實(shí)際上,是通過鏈來遍歷的慧起,不支持隨機(jī)訪問
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 remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
clear
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
//清除操作菇晃,也是遍歷鏈
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
set
//鏈?zhǔn)浇Y(jié)構(gòu)在替換上性能沒有順序存儲的性能高
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
insert
//這是一個插入方法,在性能上會比順序存儲高很多蚓挤,畢竟沒有擴(kuò)容復(fù)制的問題
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
Iterator
//迭代器的便捷使用的類磺送, 看起來很簡單, 隊(duì)列接口中使用的
private class DescendingIterator implements Iterator<E> {
private final ListItr itr = new ListItr(size());
public boolean hasNext() {
return itr.hasPrevious();
}
public E next() {
return itr.previous();
}
public void remove() {
itr.remove();
}
}
//迭代器的操作其實(shí)灿意,差不多估灿,通過看構(gòu)造函數(shù),就能大概知道是如何實(shí)現(xiàn)的了
private class ListItr implements ListIterator<E> {
//最后一個被遍歷到的節(jié)點(diǎn)元素缤剧,只有先被迭代器訪問到的元素馅袁,才能修改
private Node<E> lastReturned;
private Node<E> next;
private int nextIndex;
//同樣,也是不支持并發(fā)處理數(shù)據(jù)荒辕,這里已經(jīng)做了修改記錄
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;
}
public boolean hasPrevious() {
return nextIndex > 0;
}
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
public int nextIndex() {
return nextIndex;
}
public int previousIndex() {
return nextIndex - 1;
}
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
//看這個代碼汗销,只有之前被訪問到芒粹,才能修改
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}