上篇我們分析了arraylist的底層實現,知道了arraylist底層是基于數組實現的,因此具有查找修改快而插入刪除慢的特點。本篇介紹的linkedlist是list接口的另一種實現,它的底層是基于雙向鏈表實現的,因此它具有插入刪除快而查找修改慢的特點,此外,通過對雙向鏈表的操作還可以實現隊列和棧的功能。linkedlist的底層結構如下圖所示。
f表示頭結點引用,l表示尾結點引用,鏈表的每個結點都有三個元素,分別是前繼結點引用(p),結點元素的值(e),后繼結點的引用(n)。結點由內部類node表示,我們看看它的內部結構。
1
2
3
4
5
6
7
8
9
10
11
12
|
//結點內部類 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; } } |
node這個內部類其實很簡單,只有三個成員變量和一個構造器,item表示結點的值,next為下一個結點的引用,prev為上一個結點的引用,通過構造器傳入這三個值。接下來再看看linkedlist的成員變量和構造器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//集合元素個數 transient int size = 0 ; //頭結點引用 transient node<e> first; //尾節點引用 transient node<e> last; //無參構造器 public linkedlist() {} //傳入外部集合的構造器 public linkedlist(collection<? extends e> c) { this (); addall(c); } |
linkedlist持有頭結點的引用和尾結點的引用,它有兩個構造器,一個是無參構造器,一個是傳入外部集合的構造器。與arraylist不同的是linkedlist沒有指定初始大小的構造器。看看它的增刪改查方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
//增(添加) public boolean add(e e) { //在鏈表尾部添加 linklast(e); return true ; } //增(插入) public void add( int index, e element) { checkpositionindex(index); if (index == size) { //在鏈表尾部添加 linklast(element); } else { //在鏈表中部插入 linkbefore(element, node(index)); } } //刪(給定下標) public e remove( int index) { //檢查下標是否合法 checkelementindex(index); return unlink(node(index)); } //刪(給定元素) public boolean remove(object o) { if (o == null ) { for (node<e> x = first; x != null ; x = x.next) { if (x.item == null ) { unlink(x); 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 set( int index, e element) { //檢查下標是否合法 checkelementindex(index); //獲取指定下標的結點引用 node<e> x = node(index); //獲取指定下標結點的值 e oldval = x.item; //將結點元素設置為新的值 x.item = element; //返回之前的值 return oldval; } //查 public e get( int index) { //檢查下標是否合法 checkelementindex(index); //返回指定下標的結點的值 return node(index).item; } |
linkedlist的添加元素的方法主要是調用linklast和linkbefore兩個方法,linklast方法是在鏈表后面鏈接一個元素,linkbefore方法是在鏈表中間插入一個元素。linkedlist的刪除方法通過調用unlink方法將某個元素從鏈表中移除。下面我們看看鏈表的插入和刪除操作的核心代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//鏈接到指定結點之前 void linkbefore(e e, node<e> succ) { //獲取給定結點的上一個結點引用 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++; } //卸載指定結點 e unlink(node<e> x) { //獲取給定結點的元素 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; } |
linkbefore和unlink是具有代表性的鏈接結點和卸載結點的操作,其他的鏈接和卸載兩端結點的方法與此類似,所以我們重點介紹linkbefore和unlink方法。
linkbefore方法的過程圖:
unlink方法的過程圖:
通過上面圖示看到對鏈表的插入和刪除操作的時間復雜度都是o(1),而對鏈表的查找和修改操作都需要遍歷鏈表進行元素的定位,這兩個操作都是調用的node(int index)方法定位元素,看看它是怎樣通過下標來定位元素的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//根據指定位置獲取結點 node<e> node( int 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; } } |
通過下標定位時先判斷是在鏈表的上半部分還是下半部分,如果是在上半部分就從頭開始找起,如果是下半部分就從尾開始找起,因此通過下標的查找和修改操作的時間復雜度是o(n/2)。通過對雙向鏈表的操作還可以實現單項隊列,雙向隊列和棧的功能。
單向隊列操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//獲取頭結點 public e peek() { final node<e> f = first; return (f == null ) ? null : f.item; } //獲取頭結點 public e element() { return getfirst(); } //彈出頭結點 public e poll() { final node<e> f = first; return (f == null ) ? null : unlinkfirst(f); } //移除頭結點 public e remove() { return removefirst(); } //在隊列尾部添加結點 public boolean offer(e e) { return add(e); } |
雙向隊列操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//在頭部添加 public boolean offerfirst(e e) { addfirst(e); return true ; } //在尾部添加 public boolean offerlast(e e) { addlast(e); return true ; } //獲取頭結點 public e peekfirst() { final node<e> f = first; return (f == null ) ? null : f.item; } //獲取尾結點 public e peeklast() { final node<e> l = last; return (l == null ) ? null : l.item; } |
棧操作:
1
2
3
4
5
6
7
8
9
|
//入棧 public void push(e e) { addfirst(e); } //出棧 public e pop() { return removefirst(); } |
不管是單向隊列還是雙向隊列還是棧,其實都是對鏈表的頭結點和尾結點進行操作,它們的實現都是基于addfirst(),addlast(),removefirst(),removelast()這四個方法,它們的操作和linkbefore()和unlink()類似,只不過一個是對鏈表兩端操作,一個是對鏈表中間操作。可以說這四個方法都是linkbefore()和unlink()方法的特殊情況,因此不難理解它們的內部實現,在此不多做介紹。到這里,我們對linkedlist的分析也即將結束,對全文中的重點做個總結:
1. linkedlist是基于雙向鏈表實現的,不論是增刪改查方法還是隊列和棧的實現,都可通過操作結點實現
2. linkedlist無需提前指定容量,因為基于鏈表操作,集合的容量隨著元素的加入自動增加
3. linkedlist刪除元素后集合占用的內存自動縮小,無需像arraylist一樣調用trimtosize()方法
4. linkedlist的所有方法沒有進行同步,因此它也不是線程安全的,應該避免在多線程環境下使用
5. 以上分析基于jdk1.7,其他版本會有些出入,因此不能一概而論。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/liuyun1995/p/8287707.html