本文實例為大家分享了java實現單鏈表、雙向鏈表的相關代碼,供大家參考,具體內容如下
java實現單鏈表:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
package code; class Node { Node next; int data; public Node( int data) { this .data=data; } } class LinkList { Node first; //頭部 public LinkList() { this .first= null ; } public void addNode(Node no) { no.next=first; first=no; //在頭部添加 } public void delectNode() { Node n=first.next; first= null ; first=n; //在頭部刪除 } //刪除指定位置 public int Number() { int count= 1 ; //查看有多少元素 Node nd=first; while (nd.next!= null ) { nd=nd.next; count++; } return count; } public void delectExact( int n) { //刪除指定位置 if (n> 1 ) { int count= 1 ; Node de=first; while (count<n- 1 ) { de=de.next; count++; } de.next=de.next.next; } else first=first.next; } public void addExact( int n,Node nd) { if (n> 1 ) //添加指定位置 { int count= 1 ; Node de=first; while (count<n- 1 ) { de=de.next; count++; } nd.next=de.next; de.next=nd; } else first=first.next; } public int findNode( int n) { int count= 1 ; //查找一個數對應的位置 Node de=first; while (de.data!=n) { de=de.next; count++; if (de== null ) { return - 1 ; } } return count; } public void print() { Node no=first; //打印所有 while (no!= null ) { System.out.println(no.data); no=no.next; } } } public class TextNode { public static void main(String[] args) { LinkList ll= new LinkList(); ll.addNode( new Node( 12 )); ll.addNode( new Node( 15 )); ll.addNode( new Node( 18 )); ll.addNode( new Node( 19 )); ll.addNode( new Node( 20 )); /*System.out.println(ll.first.data); ll.delectNode(); System.out.println(ll.first.data);*/ System.out.println(ll.Number()); ll.delectExact( 3 ); ll.addExact( 3 , new Node( 100 )); System.out.println(ll.Number()); // ll.print(); System.out.println(ll.findNode( 112 )); } } |
java實現雙向鏈表:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
public class DoubleLink { public static void main(String[]args) { Node2 no= new Node2( 5 ); no.addLeft( new Node2( 6 )); no.addRight( new Node2( 7 )); /*no.print(); no.print2();*/ no.addExact2(1, new Node2(8)); no.print(); System.out.println("--------------"); no.print2(); } } class Node2 { public Node2 first; public Node2 end; public Node2 left; public Node2 right; int data=0; public Node2(int n) { first=this; end=this; first.data=n; } //從頭部添加 public void addLeft(Node2 before) { first.left=before; before.right=first; first=before; } //從尾部添加 public void addRight(Node2 after) { end.right=after; after.left=end; end=after; } //插入正數(第三聲)的第幾個 public void addExact(int n,Node2 no) { int count=0; if(n==0) { addLeft(no); } else { Node2 f=first; while(true) { f=f.right; count++; if(count==n) { //此處為四個指針的指向的變化 no.left=f.left; f.left.right=no; // first.left=no; no.right=f; f.left=no; break; } } } } //插入倒數的第幾個 public void addExact2(int n,Node2 no) { int count=0; if(n==0) { addRight(no); } else { Node2 f=end; while(true) { f=f.left; count++; if(count==n) { no.left=f; no.right=f.right; f.right.left=no; f.right=no; break; } } } } //正序遍歷 public void print() { System.out.println(first.data); while(first.right!=null) { System.out.println(first.right.data); first=first.right; } // System.out.println(end.data); } //倒序遍歷 public void print2() { System.out.println(end.data); while(end.left!=null) { System.out.println(end.left.data); end=end.left; } } } /*值得注意的是,每一次插入一個新的對象的時候,需要注意指針指向的改變。 首先是這個新的對象兩邊的指向(左和右),其次是時左邊的對象向右的指向 和右邊對象向左的指向。 這四個指針的指向必須正確,否則可能導致正序或者倒序遍歷無法實現。 */ /*對比單鏈表,單鏈表只能從一個方向遍歷,因為只有一個頭,而雙向鏈表,有頭和尾,可以從 * 頭遍歷,也可以從尾遍歷,而且其中一個對象因為有兩個方向的指針,所以他可以獲得左邊的 * 對象也可以獲得右邊的對象。 * 但是單鏈表的話,因為只有一個方向,所以只能向左或右。添加對象的時候,雙向也可以從頭添加,也可以從尾添加。 * 如果單鏈表要實現兩個方向添加比較難得,或者說不行,因為他只有向左或向右的一個方向的指針 * 而雙向鏈表每個對象都有兩個方向的指針沒這樣更靈活,但是這同樣有缺點,因為這樣的話每個對象 * 都會包含兩個指針,這同樣內存會消耗更多。 * * */ |
以上就是本文的全部內容,希望對大家學習java程序設計有所幫助。