LinkedList源码
简介
LinkedList是一个实现了List接口和Deque接口的双端链表。 LinkedList底层的链表结构使它支持高效的插入和删除操作,另外它实现了Deque接口,使得LinkedList类也具有队列的特性; LinkedList不是线程安全的,如果想使LinkedList变成线程安全的,可以调用静态类Collections类中的synchronizedList方法:
内部私有类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。这个类有三个属性,分别是前驱节点,本节点的值,后继结点。
构造方法
//默认空构造方法
public LinkedList() {
}
//使用已有集合的构造方法
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
私有方法
//将元素e作为第一个节点插入
private void linkFirst(E e) {
final Node<E> f = first;
//创建一个节点,前驱节点为null,后驱节点为first
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
//将元素e作为最后一个节点的值插入
void linkLast(E e) {
final Node<E> l = last;
//创建一个新节点,前驱节点为last,后驱节点为null
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
//在一个节点之前插入元素e的节点
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++;
}
//解除链表中第一个非空节点
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;
}
//解除链表中非空的最后一个节点
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;
}
//删除链表的一个中间节点
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;
}
公共方法
//得到链表的头节点
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
//得到链表的尾节点
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
//删除链表的头节点
public E removeFirst() {
final Node<E> f = first;
//检验first是否为空
if (f == null)
throw new NoSuchElementException();
//调用unlinkFirst(f);方法
return unlinkFirst(f);
}
//删除链表的尾节点
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
//调用unlinkLast(l)方法
return unlinkLast(l);
}
add系列
//将一个元素插入到链表的头部
public void addFirst(E e) {
linkFirst(e);
}
//将一个元素插入到链表的尾部
public void addLast(E e) {
linkLast(e);
}
//将一个元素默认插入到链表的尾部,返回true或false
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) {
//检查index是否在size中
checkPositionIndex(index);
//toArray()方法把集合的数据存到对象数组中
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
//得到插入位置的前驱节点和后继节点
Node<E> pred, succ;
//如果插入位置为尾部,前驱节点为last,后继节点为null
if (index == size) {
succ = null;
pred = last;
} else {
//否则,调用node()方法得到后继节点,再得到前驱节点
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;
}
//如果插入节点在链表尾,重置last节点
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);
if (index == size)
//如果插入位置是尾部
linkLast(element);
else
//否则,调用linkBefore(element, node(index)),在一个节点前插入
linkBefore(element, node(index));
}
一般方法
根据索引获取数据方法
public E get(int index) {
//检查index范围是否在size之内
checkElementIndex(index);
//调用Node(index)去找到index对应的node然后返回它的值
return node(index).item;
}
获取头节点(index=0)数据方法:
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
public E element() {
return getFirst();
}
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
区别: getFirst(),element(),peek(),peekFirst() 这四个获取头结点方法的区别在于对链表为空时的处理,是抛出异常还是返回null,其中getFirst() 和element() 方法将会在链表为空时,抛出异常
element()方法的内部就是使用getFirst()实现的。它们会在链表为空时,抛出NoSuchElementException
获取尾节点(index=-1)数据方法:
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
两者区别: getLast() 方法在链表为空时,会抛出NoSuchElementException,而peekLast() 则不会,只是会返回 null。
根据索引获取对象方法
//从头遍历获取
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;
}
//从尾遍历获取对象
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;
}
检查链表是否包含此对象
public boolean contains(Object o) {
return indexOf(o) != -1;
}
删除节点方法
remove() ,removeFirst(),pop(): 删除头节点
public E pop() {
return removeFirst();
}
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
removeLast(),pollLast(): 删除尾节点
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
区别: removeLast()在链表为空时将抛出NoSuchElementException,而pollLast()方法返回null。
remove(Object o): 删除指定元素
//删除一个节点方法
public boolean remove(Object o) {
if (o == null) {
//如果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;
}
当删除指定对象时,只需调用remove(Object o)即可,不过该方法一次只会删除一个匹配的对象,如果删除了匹配对象,返回true,否则false。
remove(int index):删除指定位置的元素
public E remove(int index) {
//检查index范围
checkElementIndex(index);
//将节点删除
return unlink(node(index));
}
评论区