public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
AbstractSequentialList:功能主要继承者,主要实现依靠ListIterator,ListIterator继承Iterator,LinkedList中需要实现ListIterator中的方法,避免父类复杂度。
Deque:双向列表继承
不支持同步修改:主要依靠modCount即fail-fast机制
transient int size = 0; 大小
transient Node first; 首节点
transient Node last;末节点
Node类代码如下:
private static class Node {
E item;
Node next;
Node prev;
Node(Node prev, E element, Node next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
定义前中后三个对象
public LinkedList() {
}
public LinkedList(Collection extends E> c) {
this();
addAll(c);
}
private void linkFirst(E e) {
final Node f = first;
final Node newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
作用:将e作为第一个元素
解析:首先获取原第一节点f,构建newNode,此节点前元素为null,中为e,后元素为f,将newNode作为第一个节点,若f为空则LinkedList只有newNode一个节点,否则原第一个节点f的前元素指向newNode。
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++;
}
作用:将e作为最后一个元素。
解析:大致原理同linkFirst相同,构建的newNode 后元素为null,区别两者用不同的访问级别private和protected,暂未明白其中原理,两者都不能被外界调用。
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++;
}
作用:在一个节点前插入元素e
解析:获得当前节点的前元素,构建newNode ,其中e的后元素为succ,将succ原前元素指向newNode ,e的前元素为pred,原pred的后元素还是指向succ节点,所以pred.next = newNode;若pred 为null则succ为first节点,最好画图理解。
private E unlinkFirst(Node f) {
// assert f == first && f != null;
final E element = f.item;
final Node 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;
}
作用:去除第一个非空节点
解析:将节点中元素置空,后元素置空,后节点至成first节点,first节点特点,前元素为null,若next为空则LinkedList为空,并返回中元素,外部不可调用
private E unlinkLast(Node l) {
// assert l == last && l != null;
final E element = l.item;
final Node 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;
}
作用:去除最后一个非空节点
解析:原理同unlinkFirst差不多,last特点后元素为null所以prev.next = null;
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;
}
作用:取出一个非空节点
原理:大致同上相同,prev == null,若x的前节点为空,则x为first节点,x置空后则它的后节点为first节点,next的前节点为x,x置空后为null,next满足作为fist节点要求(前元素为null),由此可知当last,first同时为null时LinkedList为空,同是指向一个节点LinkedList只有一个元素。此方法不可外部调用
public E getFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
作用:返回first节点中元素
public E getLast() {
final Node l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
作用:返回last中元素
public E removeFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
作用:移除第一个节点,返回中元素
解析:参考unlinkFirst
public void addFirst(E e) {
linkFirst(e);
}
作用:同linkFirst
public void addLast(E e) {
linkLast(e);
}
作用:同linkLast
public boolean contains(Object o) {
return indexOf(o) != -1;
}
作用:是否包含元素o
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
作用:返回此元素在结合中的第一个下标,若无为-1
public boolean add(E e) {
linkLast(e);
return true;
}
作用:在结合末尾添加一个元素
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;
}
作用:删除o在集合中第一次出现的位置
解析:参考unlink
public boolean addAll(Collection extends E> c) {
return addAll(size, c);
}
作用:将集合c插入LinkedList中,正序返回
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 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 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;
}
作用:从指定位置开始将集合c插入到LinkedList中
解析:checkPositionIndex使index最大只能为LinkedList的size,
public Object clone() {
LinkedList clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}
作用:LinkedList浅拷贝
public 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 x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}