public LinkedList(Collection<? extends E> c) {
this();
addAll(c); // 后面会介绍
}
1
2
3
4
5
6
7
相关方法
增删改查
link(add)
// 在头节点前插入元素
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f); // 构建新节点
first = newNode;
if (f == null) // 第一次插入节点
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
// 在尾节点后插入元素
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++;
}
// 在succ点后前插入元素
// 调用此方法时应该保证succ不为空
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) // 如果succ是头结点
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
// 头结点前插入元素
public void addFirst(E e) {
linkFirst(e);
}
// 尾结点后插入元素
public void addLast(E e) {
linkLast(e);
}
// 将集合插入到链表的尾部
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
// 检查是否越界
// 将集合插入到index位置
public boolean addAll(int index, Collection<? extends E> c) {
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;
}
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;
}
// 在指定位置插入元素
public void add(int index, E element) {
checkPositionIndex(index);
// 删除头结点,并返回头结点的值
// 调用此方法时应该保证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;
}
// 删除尾结点,并返回尾结点的值
// 调用此方法时应该保证l执行尾结点,并且尾结点不为空
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null) // 只有一个节点的时候
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
// 删除节点x,并返回接单x的值
// 调用此方法时应该保证x不为空
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) { // 如果x为头结点
first = next;
} else {
prev.next = next;
x.prev = null;
}
if (next == null) { // 如果x为尾结点
last = prev;
} else {
next.prev = prev;
x.next = null;
}
// 删除头结点
// 结点为空时抛错
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
// 删除尾结点
// 结点为空时抛错
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
// 删除元素o
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) { // 链表访问也可以用for,以前都是用while,搞的一团糟
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 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++;
}
// 删除指定位置的元素
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
// 删除第一次出现value为o的元素
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}
if (index < (size >> 1)) { // 如果index在前半部分
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else { // 如果index在后半部分
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
// 根据valu检索第一次出现的位置
// 跟remove(Object)很类似
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;
}
// 根据valu检索最后一次出现的位置
// 跟remove(Object)很类似
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
set
// 设置位置index上的value
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
1
2
3
4
5
6
7
8
越界判断
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;