LinkedList源码分析
Java中的LinkedList类实现了List接口和Deque接口,是一种链表类型的数据结构,支持高效的插入和删除操作,同时也实现了Deque接口,使得LinkedList类也具有队列的特性。LinkedList类的底层实现的数据结构是一个双端的链表。
LinkedList类中有一个内部私有类Node,这个类就代表双端链表的节点Node。这个类有三个属性,分别是前驱节点,本节点的值,后继节点。
Node源码:
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;
}
}
双端链表由node组成,每个节点有两个引用指向前驱节点和后继节点,第一个节点的前驱节点为null,最后一个节点的后继节点为null
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
12345678910111213
first和last需要维持一个不变量,分别指向头结点和尾节点,也就是first和last始终都要维持两种状态:如果双端链表为空的时候,两个都必须为null;如果链表不为空,那么first的前驱节点一定是null,first的item一定不为null,同理,last的后继节点一定是null,last的item一定不为null。
新增方法
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
//在最后面添加元素
linkLast(e);
return true;
}
/**
* 插入到last节点的后面
* Links e as last element.
*/
void linkLast(E e) {
//取到添加前的最后一个元素
final Node<E> l = last;
//创建一个新的节点,并且将此节点之前的一个元素,新元素的传入
//由于是最后一个元素,所以其后面的元素时空的,因此后一个元素为null
final Node<E> newNode = new Node<>(l, e, null);
//更新最后一个元素的引用
last = newNode;
if (l == null)//f == null,表示此时LinkedList为空
first = newNode;//将新创建的节点赋值给first
else
l.next = newNode;
size++;//刷新总数
modCount++;
}
/**
* 插入第一个节点
* Links e as first element.
*/
//在first节点的前面插入一个节点,插入完之后,还要更新first节点为新插入的节点,并且同时维持last节点的不变量。
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++;
}
分析代码,首先用f来临时保存未插入前的first节点,然后调用的node的构造函数新建一个值为e的新节点,这个节点插入之后将作为first节点,所以新节点的前驱节点为null,值为e,后继节点是f,也就是未插入前的first节点。
然后就是维持不变量,首先第一种情况,如果f==null,那就说明插入之前,链表是空的,那么新插入的节点不仅是first节点还是last节点,所以我们要更新last节点的状态,也就是last现在要指向新插入的newNode。
如果f!=null那么就说明last节点不变,但是要更新f的前驱节点为newNode,维持first节点的不变量。
最后size加一就完成了操作。