LinkedList是基于双链表实现的。
双链表相对与普通链表,多了个prev 前驱的指针。用于指向前一个元素。
插入基于虚拟头节点。统一为中间插入
基于虚拟头节点。统一为中间删除
通过IDEA生成LinkedList类图。LinkedList实现了List(线性表)、Queue(队列)、Deque(双端队列)接口。
- AbstractList
protected transient int modCount = 0;
- LinkedList
// 节点个数。冗余存储,防止遍历
transient int size = 0;
transient Node first;
transient Node last;
// 序列化id,用于反序列化验证。无需关系
private static final long serialVersionUID = 876323262645176354L;
实例化
- AbstractCollection
protected AbstractCollection() {
}
- AbstractList
protected AbstractList() {
}
- AbstractSequentialList
protected AbstractSequentialList() {
}
- LinkedList
public LinkedList() {
}
public LinkedList(Collection extends E> c) {
this();
addAll(c);
}
方法
- 作为List
尾插
由于没有虚拟头节点。尾插,分为两种情况(first == null , first != null)。
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node l = last;
final Node 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));
}
void linkBefore(E e, Node succ) {
// assert succ != null;
final Node pred = succ.prev;
final Node newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
Node node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
删除
指定位置
定位节点需要遍历,性能差。
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
E unlink(Node x) {
// assert x != null;
final E element = x.item;
final Node next = x.next;
final Node 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;
}
Node node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
指定值
定位节点需要遍历,性能差。
public boolean remove(Object o) {
if (o == null) {
for (Node x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node 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 x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
清空
遍历
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 x = first; x != null; ) {
Node next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
查询
查询大量依赖遍历,性能差
// 定位节点需要遍历,性能差。
public boolean contains(Object o);
// 定位节点需要遍历,性能差。
public E get(int index);
// 定位节点需要遍历,性能差。
public int indexOf(Object o);
// 定位节点需要遍历,性能差。
public int lastIndexOf(Object o);
// 冗余变量,不需要遍历
public int size();



