JAVA-LinkedList 源码学习-201805
(jdk 8 源码)
一、概念
List 接口的链表实现。实现所有可选的列表操作,并且允许所有元素(包括 null)。其大小理论上只受内存大小影响。除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。
此类实现 Deque 接口,为 add、poll 提供先进先出队列操作,以及其他堆栈和双端队列操作。
注意,此实现不是同步的。如果多个线程同时访问一个链接列表,而其中至少一个线程从结构上修改了该列表,则它必须 保持外部同步。
二、源码
1.实现的接口及继承的类
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
实现了List、Deque、Clonable、Serializable接口,继承自AbstractSequentialList,注意到该类与ArrayList实现接口区别在于ArrayList实现了RandomAccess接口,随机访问;而LinkedList未实现该接口,是由其数据结构决定的。同样,AbstractSequentialList这个类只支持按序访问,不支持随机访问。
2.成员变量
transient int size = 0; //list中元素个数
/**
* 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; //尾结点-注释恒等式
3.构造方法
/**
* Constructs an empty list.
*/
public LinkedList() { //构造空list
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection<? extends E> c) { //这里直接调用addAll(c)构造
this();
addAll(c);
}
4.Node类-节点(指向prev的指针,本节点实际元素,指向next节点的指针)
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;
}
}
void linkFirst(E e)--链入e作为头结点
/**
* Links e as first element.
*/
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f); //newNode结点设为首节点-prev位为null
first = newNode;
if (f == null)
last = newNode; //若原首节点为null,则原list为null,首节点即为尾节点
else
f.prev = newNode; //若原list不为null,则将原首节点f设为第二节点
size++; //元素个数+1
modCount++; //结构性修改+1
}
void linkLast(E e)--链入e作为尾结点-具体实现与linkFirst(E e)类似
/**
* Links e as last element.
*/
private void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null); //尾结点的next位设为null
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
void linkBefore(E e,Nord<E> succ)-在非空节点succ前插入元素E
/**
* Inserts element e before non-null Node succ.
*/
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); //将e构造为节点prev位:pred,next位:succ
succ.prev = newNode; //将succ的prev位改为newNode
if (pred == null) //判断pred是否为null,若是,则list为空,e为第一个nord
first = newNode; //若不是,则将pred的next位设为newNode
else
pred.next = newNode;
size++;
modCount++;
}
private E unlinkFirst(Node<E> f)--断开与头节点f的链接,返回f.element
/**
* Unlinks non-null first node f.
*/
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next; //将f的next位和element均赋null
f.item = null;
f.next = null; // help GC //将原第二节点赋为头节点,若第二节点也为null,则表示原list只有一个元素
first = next; //此时断开与原头结点链接后,list为null;若第二节点非空,则将该节点的prev位设为null,
if (next == null) //作头结点。
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
private E unlinkLast(Node<E> l) -断开与尾节点l的链接,返回l.element(实现与unlinkFirst()类似)
/**
* Unlinks non-null last node 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;
}
E unlink(Node<E> x)--断开与非空节点x的链接
/**
* Unlinks non-null node 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) { //将x的前节点prev.next=x.next;x的后节点next.prev=x.prev;
first = next; //再排除list只有x一个元素的情况
} 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;
}
Node<E> node(int index) --返回指定index位置的Nord
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++) //若index<size/2,则从前向后遍历通过x.next取得index位置Nord
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--) //若index>=size/2,则从后往前遍历,通过Nord.prev指针向前寻到index位Nord
x = x.prev;
return x;
}
}
5. public E getFirst()--返回第一个元素(直接取头结点.item)
public E getLast()--返回最后一个元素(直接取尾结点.item)
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()-移除第一个元素(直接调用unlinkFirst)
public E removeLast()-移除最后一个元素(直接调用unlinkLast)
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
public void addFirst(E e)-添加e为第一个元素(直接调用linkFirst)
public void addLast(E e)-添加e为最后一个元素(直接调用linkLast)
public void addFirst(E e) {
linkFirst(e);
}
public void addLast(E e) {
linkLast(e);
}
public boolean add(E e)-添加e为最后一个元素(直接调用linkLast)-永远返回true
public boolean add(E e) {
linkLast(e);
return true;
}
public int size()-返回元素个数size
public int size() {
return size;
}
6.public int indexOf(Object o)--返回元素o第一次出现的位置
/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index {@code i} such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*
* @param o element to search for
* @return the index of the first occurrence of the specified element in
* this list, or -1 if this list does not contain the element
*/
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) { //分o==null与o!=null两种情况讨论,若list中不存在该元素则返回-1
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)--返回元素o最后一次出现的位置(与上面的方法类似,从后向前遍历)
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;
}
7.public boolean contains(Object o)-返回list中是否存在o
public boolean contains(Object o) {
return indexOf(o) != -1; //若indexOf(o)==-1,则list中无此元素
}
8.public boolean remove(Object o)-删除第一次出现的o
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) { //分o==null和!=null两种情况讨论
if (x.item == null) { //使用unlink(o)解除与o所在节点的链接
unlink(x); //若list中存在o,返回true;反之,返回false
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)-按index删除元素(使用unlink解除链接)
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
public void clear() -清除list中所有元素
/**
* Removes all of the elements from this list.
* The list will be empty after this call returns.
*/
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
for (Node<E> x = first; x != null; ) { //直接遍历把nord的各属性均赋null
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
9.public E get(int index)--取得index位置的元素
public E get(int index) {
checkElementIndex(index); //越界检查
return node(index).item; //调用nord(index)返回该index位置的nord,再取其元素item
}
10.public E set(int index, E element)-修改指定位置index为元素E-返回原值
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index); //直接用node(index)定位到该节点,再改变其element属性
E oldVal = x.item;
x.item = element;
return oldVal;
}
11.public Object[] toArray()--将list元素转成Object[]
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next) //直接遍历
result[i++] = x.item;
return result;
}
public <T> T[] toArray(T[] a) -
将list元素转到T[] a内
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size); //若a的长度<list中元素个数,通过反射类Array的newInstance
int i = 0; //方法构造一个T[size]数组:参数(数组组件的类型,数组长度)。
Object[] result = a; //原因是java不支持直接new T[10]泛型数组,通不过编译;
for (Node<E> x = first; x != null; x = x.next) //遍历list将x.item输入数组
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
12.public boolean addAll(int index, Collection<? extends E> c)-将c中元素全部添加到index位置
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index); //越界检查,注意这里index可以取[0,size]
Object[] a = c.toArray(); //c转成数组,若为空,返回false;反之,返回true
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) { //若在队尾add,令该collection插入位前一个nord:pred为原last,后一个nord:succ为null
succ = null;
pred = last;
} else { //若不在队尾add,则该collection插入位后一个Nord:succ直接调nord(index)返回,前一个Nord:pred为nord(index).prev
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o; //遍历a数组,先强转o为E类型
Node<E> newNode = new Node<>(pred, e, null); //new一个Nord,前驱节点赋pred,元素位赋e,后继节点赋null;
if (pred == null) //若pred为null,插入位为0,第一个newNord为头结点;
first = newNode; //若pred不为null,将pred的后继节点设为newNord;
else //最后把现在的newNode设为下一次循环生成的newNode的前驱节点
pred.next = newNode;
pred = newNode;
}
if (succ == null) { //以上循环已把collection的元素链入list
last = pred; //若succ为null,在队尾插入,则把最后一次循环生成的pred=newNode赋给last
} else {
pred.next = succ; //若不在队尾插入,则把最后一次循环生成的pred=newNord的后继节点赋为succ,
succ.prev = pred; //succ的前驱节点赋pred
}
size += numNew; //修改list的size
modCount++;
return true;
}
public boolean addAll(Collection<? extends E> c)--在队尾添加c内元素(直接调上面的方法)
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
13.队列操作Queue
public E peek()--出队(从前端)-取头结点的element,若头结点为null,返回null
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item; //这里与getFirst()的区别是若f=null,不会抛异常
}
public E element()-与getFirst()完全相同
public E element() {
return getFirst();
}
public E poll()-出队(从前端)-取头结点的element并删除头结点,若头结点为null,返回null
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public boolean offer(E e)-与add(E e)完全一样
public boolean offer(E e) {
return add(e);
}
public E remove(int index)-在上面写过了
14.双端队列操作-Deque
public boolean offerFirst(E e)-入队从前端
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
public boolean offerLast(E e)-入队从后端
public boolean offerLast(E e) {
addLast(e);
return true;
}
public E peekFirst()-出队从前端-若头结点为null,返回null
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekLast()-出队从后端-若尾结点为null,返回null
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
public E pollFirst()-出队从前端并删除该结点-若尾结点为null,返回null
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pollLast()-出队从后端并删除该结点-若尾结点为null,返回null
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
15.栈操作
public void push(E e)-入栈-从前端添加
public void push(E e) {
addFirst(e);
}
public E pop()-出栈-从前端删除
public E pop() {
return removeFirst();
}
参考:
https://blog.youkuaiyun.com/anxpp/article/details/51203591