LinkedList原始碼詳解

2021-09-28 23:51:01 字數 3867 閱讀 9828

說明:linkedlist底層是用雙鏈表的方式實現,比較適合add和remove操作場景較多的情況。

雙鏈表結構圖(類似於自行車鏈條,一環扣一環):

一)類圖

linkedlist繼承了abstractsequentiallist抽象類,實現了add、remove、set等功能。

linkedlist繼承了list介面,依賴與iterator和listiterator介面,實現了資料迭代等功能。

linkedlist繼承了deque介面,可以實現雙佇列的一些操作。

備註:如需自己實現linkedlist類似的功能,可以繼承linkedlist的父類,然後再自定義方法。

二)構造方法

1)linkedlist(),初始化乙個集合物件

2)linkedlist(collection extends e> c),構造乙個指定元素集合的列表,其實就是呼叫了addall(collection extends e> c)方法

// 主要資料結構

transient nodefirst; // 指向頭部元素指標

transient nodelast; // 指向尾部元素指標

transient int size = 0; // 集合的元素個數,預設為0

// 雙鏈表結構

private static class node

}

三)新增1)offer(e e),在尾部添元素

add(e e),在尾部添元素

offerlast(e e),在尾部添元素

addlast(e e),在尾部添元素

public boolean offer(e e) 

public boolean add(e e)

public boolean offerlast(e e)

public void addlast(e e)

void linklast(e e)

2)offerfirst(e e),在頭部新增元素

push(e e),在頭部新增元素

addfirst(e e),在頭部新增元素

public boolean offerfirst(e e) 

public void push(e e)

public void addfirst(e e)

private void linkfirst(e e)

3)add(int index, e element),在指定的index位置,新增新元素

public void add(int index, e element) 

nodenode(int index) else

}void linkbefore(e e, nodesucc)

4)addall(collection extends e> c),新增乙個指定元素集合的列表

addall(int index, collection extends e> c),在指定index下標位置,新增乙個指定元素集合的列表

public boolean addall(collection extends e> c) 

public boolean addall(int index, collection extends e> c) else

for (object o : a)

if (succ == null) else

size += numnew;

modcount++;

return true;

}

四)查詢1)get(int index),根據指定index下標查詢元素,元素不存在會報錯

2)getfirst(),獲取列表中第乙個元素

element(),獲取列表中第乙個元素,呼叫了getfirst()方法

peek(),獲取列表中第乙個元素,元素不存在返回null

peekfirst(),獲取列表中第乙個元素,元素不存在返回null

3)getlast(),獲取列表中最後乙個元素,元素不存在會報錯

peeklast(),獲取列表中最後乙個元素,元素不存在返回null

4)indexof(object o),根據指定元素的值,從頭部元素中開始查詢第乙個出現的元素返回

public int indexof(object o) 

} else

}return -1;

}

5)lastindexof(object o),根據指定元素的值,從尾部元素中開始查詢第乙個出現的元素返回

public int lastindexof(object o) 

} else

}return -1;

}

五)修改1)set(int index, e element),修改指定index下標位置的元素,替換成element

public e set(int index, e element)
六)刪除1)remove(),查詢到第乙個元素,並刪除

pop(),查詢到第乙個元素,並刪除

removefirst(),查詢到第乙個元素,並刪除,元素不存在會報錯

poll(),查詢到第乙個元素,並刪除

pollfirst(),查詢到第乙個元素,並刪除,元素不存在返回null

public e remove() 

public e pop()

public e removefirst()

public e poll()

public e pollfirst()

private e unlinkfirst(nodef)

2)removelast(),找到最後乙個元素,並刪除,元素不存在報錯

polllast(),找到最後乙個元素,並刪除,元素不存在返回null

public e removelast() 

public e polllast()

private e unlinklast(nodel)

七)迭代備註:linkedlist的迭代方式和arraylist、vector一樣。

八)總結

1、linkedlist底層是用雙鏈表的方式實現。

2、linkedlist比較適合add和remove操作場景較多的情況,根據元素查詢到位置,只需要改變元素的prev和next指向。

3、根據linkedlist的特性,可以做各種效果,比如說需獲取乙個元素,可以呼叫返回值為null的方法,也可以呼叫返回值報錯的方法。

LinkedList原始碼詳解

linklist概述 linkedlist 是 list 介面鍊錶的實現。基於雙向鍊錶實現的方式使得 linkedlist 在插入和刪除時更優於 arraylist,而隨機訪問則比 arraylist 遜色些。但也是執行緒不安全 linklist uml類圖 linklist構造方法 構造乙個空列表...

LinkedList原始碼詳解 2

peek操作 檢視不刪除 peek peekfirst 檢視容器中的第乙個元素,但是不刪除此元素。此方法和getfisrt效果類似,但是值得注意的是如果第乙個元素為空的話getfirst會丟擲異常 public e peek public e peekfirst 2.peeklast 檢視容器最後乙...

LinkedList 原始碼分析

linkedlist資料結構是 雙向鍊錶 先來講下單鏈表和雙向鍊錶 雙向鍊錶 單鏈表相對於雙向鍊錶來說,結構簡單。但有乙個缺點,即在單鏈表中只能通過乙個節點的引用訪問其後續節點,無法直接訪問其前驅節點,如果在單鏈表中想找到某個幾點的前驅節點,必須遍歷鍊錶,耗費時間。因此擴充套件了單鏈表,在單鏈表結構...