LinkedList源码解读

时间:2023-03-08 15:49:26

一、内部类Node数据结构

在讲解LinkedList源码之前,首先我们需要了解一个内部类。内部类Node来表示集合中的节点,元素的值赋值给item属性,节点的next属性指向下一个节点,节点的previous属性指向前一个节点。

 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;
}
}

LinkedList源码解读

二、构造函数

  • 无参构造函数

  • 有参构造函数,传入Collection(****)

//无参构造函数
public LinkedList() {
} //传入Collection的构造
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}

有参构造函数实现的思想为:

  1. 默认在链表末尾插入。

  2. 将集合转换为数组,按照遍历顺序将数组首尾相连。

  3. 成员变量first指向链表第一个元素,last指向链表最后一个元素。

 public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//添加所有元素
public boolean addAll(int index, Collection<? extends E> c) {
//判断索引是否合法
checkPositionIndex(index);
//将集合转化为数组
Object[] a = c.toArray(); int numNew = a.length;
if (numNew == 0)
return false; Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
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;
}

三、常用方法

1、addFirst();

当链表为null的时候,我们添加一个元素,要记得将last也指向该该节点。这时候链表只有一个元素。

 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;
//first如果为null,则last也指向该节点
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}

2、addLast();

当链表为null的时候,我们添加一个元素,要记得将first也指向该该节点。这时候链表只有一个元素。

 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;
//当last指向为null的时候,说明first也为null,所以要将first指向
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}

LinkedList源码解读

3、getFirst()和getLast()

当first节点不为null的时候,返回first节点的值。当last节点不为null的时候,返回last节点的值。如果first或者last为null,则抛出异常。

 public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
//返回first节点的值
return f.item;
} public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
//返回last节点的值
return l.item;
}

4、get()

首先判断给定的索引值索引值是否合法。

索引值大于整个链表长度的一半,则从后往前找,若索引值小于整个链表的长度的一般,则从前往后找。这样就可以保证,不管链表长度有多大,搜索的时候最多只搜索链表长度的一半就可以找到,提升了效率。

 public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* 寻找下标为index节点
*/
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;
}
}

5、removeFirst()

删除头结点,将原来的第二个节点变为头结点,改变frist的指向,若之前仅剩一个节点,移除之后全部置为了null,包括last。

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;
}

欢迎加入Java学习群: 484757838