LinkedList源码解析
简介
基础源码比较简单,建议手写一遍。
1、重要的成员变量
transient int size = 0;
// 头结点
transient Node<E> first;
// 尾结点
transient Node<E> last;
2、构造方法
public LinkedList() {
}
3、核心方法
3.1 内部类 Node
LinkedList的底层是由一个个node结点组成,每一个node结点都有三个属性:上一个结点prev,下一个结点next,以及该节点的值item。
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.2 插入元素 add(E e) 和 set(int index, E element)
add方法就是调用了插入尾结点方法 linkLast(E e)。
set方法在指定位置替换节点,同时返回被替换的节点的值。
public boolean add(E e) {
linkLast(e);
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++;
}
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
3.3 获取元素 get(int index)
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) {
// 如果index小于size的一半,即数据在前半部分,顺序遍历查询;否则倒序遍历查询。
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;
}
}
3.4 其他方法
很多方法逻辑上都大同小异,这里就不一一展示了。
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 E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
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;
}
4、手写一个简单的LinkedList
public class MyLinkedList<T> {
// 定义列表的头结点和尾结点
private Node<T> last;
private Node<T> first;
private int size;
private static class Node<T> {
// 定义结点类所需要的属性:值、上一个结点和下一个结点。
T value;
Node<T> pre;
Node<T> next;
Node(T value, Node<T> pre, Node<T> next) {
this.value = value;
this.pre = pre;
this.next = next;
}
}
public void add(T value) {
addLast(value);
}
public T get(int index) {
return getNode(index).value;
}
private Node<T> getNode(int index) {
Node<T> f;
if (index < size / 2) {
f = first;
for (int i = 0; i < index; i++) {
f = f.next;
}
} else {
f = last;
for (int i = size - 1; i > index; i--) {
f = f.pre;
}
}
return f;
}
private void addLast(T value) {
// 定义一个新的指针指向尾结点,因为后续需要将尾结点指针指向新插入的结点。
Node<T> l = last;
// 创建一个新结点,即我们新插入的结点。
Node<T> node = new Node<>(value, l, null);
// 每次将新结点插到最后
last = node;
if (l == null) {
// 首次添加,first和last都指向同一结点
first = node;
} else {
// 之前的last结点的next需指向新结点
l.next = node;
}
size++;
}
public int size() {
return size;
}
}