集合框架源码分析(jdK1.7)(六)LinkedList

六LinkedList

1 LinkedList的数据结构

LinkedList是一个双向链表。属性定义了一个头节点和一个尾节点。它不需要调整容量。

LinkedList的内部是有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;
   
}
}

2主要参数

//头结点

transient Node<E> first;

//尾节点

transient Node<E> last;

transient int size = 0;

3.构造方法

空的构造方法,没有初始容量

public LinkedList() {
}

4. add(E e)方法

public boolean add(E e) {
   
linkLast(e); //在链表的尾部添加元素
   
return true;
}

void linkLast(E e) { //可以看出来LinkedList内部是双链表的Node维护的
   
final Node<E> l = last//指向链表的尾部地址(last是链表的尾节点)
   
final Node<E> newNode = new Node<>(l, e, null);/以尾部节点为前驱节点建立新节点
   
last = newNode;//把链表的尾部指向新节点地址
   
if (l == null) //如果链表为空,那么该节点既是头节点也是尾节点
       
first = newNode;
   
else //链表不为空,那么将该结点作为原链表尾部的后继节点
       
l.next = newNode;
   
size++;
   
modCount++;
}

5.add(int index, E element)

public void add(int index, E element) {
   
checkPositionIndex(index); //检查插入的索引是否大于链表长度,大于抛异常IndexOutOfBoundsException
   
if (index == size)
       
linkLast(element);//索引等于长度尾插法
   
else
       
linkBefore(element, node(index));/在中间插入
}

//双链表的中间插入也很简单,根据索引找到前驱节点,创建新节点,该位置老节点指向新节点

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

6.get(int index)

public E get(int index) {
   
checkElementIndex(index); //检查索引是否存在
   
return node(index).item; //查找节点
}

//查找思想

如果index小于size/2,也就是在前半部分,从链表头部开始循环获取;

如果index不小于size/2,也就是在后半部分,从链表尾部开始循环获取

遍历次数最多为size/2

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

7.push(E e)方法

//push方法是将一个元素添加到链表头部。

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

8 pop()方法

//pop方法调用removeFirst方法,里面再调用unlinkFirst

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

1.   remove(intindex)

2.     //根据双链表特性直接删除

3. 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.      

10 .toArray()方法

//把双链表转化为数组,然后链表元素从头到尾按顺序放到这个数组中。

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

11.总结

1.LinkedList的数据结构是双向链表,具有双向链表的特性,插入速度快,查找速度慢,但是LinkedList在按照索引查找用了一个小技巧,先用二分法判断区间,在遍历查找

2. LinkedList是双向链表无初始容量。



猜你喜欢

转载自blog.csdn.net/zpoison/article/details/80884875