1: 结构
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
- LinkedList是一个实现了List接口和Deque接口的双端链表
- LinkedList底层的链表结构使它支持高效的插入和删除操作,
- 另外它实现了Deque接口,使得LinkedList类也具有队列的特性;
- LinkedList不是线程安全的,如果想使LinkedList变成线程安全的,可以调用静态类Collections类中的synchronizedList方法
2:属性
transient int size = 0; //节点数量
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;
}
}
3:构造方法
(1)LinkedList(): 构造一个空列表
public LinkedList() {
}
(2)构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
4:add(E e):将指定的元素追加到此列表的末尾
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
//记录当前末尾节点
final Node<E> l = last;
//产生一个新的节点 该节点的前驱节点是之前的last,后继节点是null
final Node<E> newNode = new Node<>(l, e, null);
//将last 尾指针指向新的节点
last = newNode;
if (l == null)
first = newNode; //链表中没有节点, 头指针指向新节点
else
l.next = newNode; // 当前last的next指向新节点
size++;//更新节点数量
modCount++;
}
步骤:
1:记录当前末尾节点,用添加元素产生一个新的节点
2:将last指针指向新的尾节点
3:情况1:如果之前的尾节点为空,表明链表中没有元素,头指针first指向新节点
4:情况2:如果之前的链表不为空,旧的尾节点的next指针指向新的尾节点
5:add(int index,E element):指定位置插入指定元素
public void add(int index, E element) {
checkPositionIndex(index);// 校验索引位置
if (index == size) // 在最后插入
linkLast(element);
else
linkBefore(element, node(index));// 在索引位置之前插入
}
// 折半查找,返回指定索引位置的节点
//(1)首先确定index的位置,,是靠近first还是靠近last
//(2)靠近first就从头开始查,否则就从尾部开始
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;
}
}
void linkBefore(E e, Node<E> succ) {// succ是之前index位置上的节点,将e插到succ节点前面
// assert succ != null;
final Node<E> pred = succ.prev; // 拿到succ节点的prev节点
//构造新节点,新插入节点的前驱节点是succ的前一个位置的节点,后继节点是succ节点
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;// 将succ节点的prev属性设置为newNode
if (pred == null)
first = newNode; // 之前只有一个节点
else
pred.next = newNode; //将pred节点的next属性设为newNode
size++;
modCount++;
}
6:get(int index):返回此列表中指定位置的元素
public E get(int index) {
checkElementIndex(index); // 检查index是否越界
return node(index).item; // 根据index,调用node方法寻找目标节点,返回目标节点的item
}
7:删除节点
(1)删除指定位置
public E remove(int index) {
checkElementIndex(index); //检查index index >= 0 && index < size;
return unlink(node(index));
}
(2)删除指定值的节点
public boolean remove(Object o) {
//判断删除的元素是否是null
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;
}
重点方法:unlink(Node<E> e)
E unlink(Node<E> x) {// 移除链表上的x节点
// assert x != null;
final E element = x.item; // x节点的值
final Node<E> next = x.next;// x节点的下一个节点
final Node<E> prev = x.prev;// x节点的上一个节点
if (prev == null) {//如果prev为空,则代表x节点为头节点,删除的是第一个节点,first向后移动
first = next;
} else {// x不为头节点
prev.next = next; //将prev节点的next书香指向x节点的next属性
x.prev = null; // 将x节点prev置空
}
if (next == null) { // 如果next为空,则代表x节点为尾节点,则将last指向prev即可
last = prev;
} else {// x不是尾节点
next.prev = prev; // 将next节点的prev属性指向x节点的prev属性
x.next = null;// 该节点next置空
}
x.item = null; // 将x的值清空,以便垃圾回收器回收对象
size--; // 节点个数减1
modCount++;
return element;
}
删除步骤:
(1)获取到需要删除元素当前的值,指向它前一个节点的引用,以及指向它后一个节点的引用
(2)判断删除的是否为第一个节点,若是则first向后移动,若不是则将当前节点的前一个节点next指向当前节点的后一个节点
(3)判断删除的是否为最后一个节点,若是则last向前移动,若不是则将当前节点的后一个节点的prev指向当前节点的前一个节点
(4)将当前节点的值设置为null
(5)size减少并返回删除结点的值
8:clear():清除链表的所有节点
public void clear() {
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;// 将头节点和尾节点设为null
size = 0;
modCount++;
}
9: ArrayList与LinkedList比较
相同点:
- 接口实现:都实现了List接口,都是线性列表的实现
- 都是线程不安全的
- 实现线程安全使用工具类Collections.synchronizedList(new ArrayList())方法。
区别:
- 底层实现:ArrayList内部是数组实现,LinkedList内部实现是双向链表
- 接口实现:ArrayList实现了RandomAccess可以支持随机元素访问,LinkerList实现了Deque可以当作队列使用
- 性能:ArrayList支持随机元素访问,LinkedList新增,删除快