LinkedList是实现了List和Deque接口的双端链表。ArrayList是用数组实现的,那么在查的方面肯定优于基于链表的LinkedList,与之相对的是LinkedList在增删改上优于ArrayList。
LiskedList的核心数据结构
transient Node<E> first;
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;
}
}
添加操作add和addAll
public boolean add(E e) {
linkLast(e);//在尾部添加元素
return true;
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);//检查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);//获取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 {
//将添加的最后一个元素的后继节点指向succ
pred.next = succ;
//将添加的最后一个元素的后继节点的前驱指向最后一个元素
succ.prev = pred;
}
size += numNew;
modCount++;
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++;
}
//从头部添加
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++;
}
//在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)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
//根据索引获取元素节点
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;
}
}
//删除元素
public boolean remove(Object o) {
if (o == null) {
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 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;
}
当然也提供从尾部开始的检索,与以上代码类似。
删除元素
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;
}
因为LinkedList为双端链表,既可以当成队列也可以当成栈来使用。当然LinkedList中也提供了相关方法,基本都是通过以上方法实现的就不做介绍了。
作为集合的共性LinkedList,通过实现ListIterator提供了迭代器。