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

目錄

1 LinkedList的數據結構

2主要參數

3.構造方法

4. add(E e)方法

5.add(int index, E element)

6.get(int index)

7.push(E e)方法

8 pop()方法

9. remove(intindex)

10 .toArray()方法

11.總結


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

 

 

9. 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是雙向鏈表無初始容量。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章