LinkedList源码解读
底层原理
linkedlist底层采用链表实现,
//链表长度
transient int size = 0;
//链表首
transient Node<E> first;
//链尾
transient Node<E> last;
基础结构: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;
}
}
增删改查
add
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
//原来的链尾
final Node<E> l = last;
//创建一个新的节点,prev是原来的链尾,next为null
final Node<E> newNode = new Node<>(l, e, null);
//链尾指向新的节点
last = newNode;
//如果原来的链表为null,则新插入节点为链首
if (l == null)
first = newNode;
else
l.next = newNode;//原来不为null,新节点在链尾后面
size++;
modCount++;
}
addAll
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//分为两步:
//1.集合插入到pred后面;
//2.succ放入collection后面
public boolean addAll(int index, Collection<? extends E> c) {
//检查index范围
checkPositionIndex(index);
//将collection转化为数组(保证原有顺序)
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
//pred表示的是插入位置的前一个节点;succ表示插入位置的节点
Node<E> pred, succ;
//插入到链尾
if (index == size) {
succ = null;
pred = last;
} else {
//插入到指定位置,这里首先找到index位置的节点
succ = node(index);
pred = succ.prev;
}
//循环插入多个节点,这一串节点是放在pred的next
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
//如果pred为null,说明要插入的位置是链首
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
//将succ放入插入集合的next
//如果succ,说明要插入的位置是链尾
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
//获取指定位置的节点
Node<E> node(int index) {
// assert isElementIndex(index);
//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;
}
}
remove
从链首遍历到链尾,如果节点的item等于目标节点,则删除之。
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;
}
//删除非空节点x
//1. 修改x的prev.next
//2. 修改x的next.prev
//3. 删除x
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;
}
get
进行范围检查后,调用node(index)获取
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
set
- 进行范围检查;
- 直接修改制定位置的item,并不会涉及到链表的改动哦。
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
contains
循环遍历链表中是否包含某个元素
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
//分为是否为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;
}
Deque相关
由于linkedList实现了Deque接口,Deque是“double end queue”的缩写,发音“deck”,是可以在两端进行插入和删除的线性数据结构。
下面几个方法便是实现Deque而来:
getFirst getLast
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;
}
removeFirst removeLast
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
//删掉头结点
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;
}
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
//删掉尾节点
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;
}
addFirst addLast
public void addFirst(E e) {
linkFirst(e);
}
//插入头结点
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++;
}
public void addLast(E e) {
linkLast(e);
}
//插入尾节点
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++;
}
还有peek、push等方法,只要知道内部结构(链表、prev、next等),这些方法的实现都很容易理解。