本文實例講述了java數據結構之簡單鏈表的定義與實現方法。分享給大家供大家參考,具體如下:
一、概述:
1、原理:
只有一個數據項(鏈接點link),每個數據插入時都是對第一個數據的引用。
2、插入數據說明:
當鏈表沒有數據時,插入的值就是第一個數據,如果鏈表里有數據,就把當前的數據的next指針指向第一個數據。
3、插入數據圖:
4、特點:先進后出
5、實現功能:
數據插入,指定位置插入,顯示,查詢,刪除等
6、刪除原理
7、插入頭節點原理
二、實現:
1、創建節點
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
|
/** * @描述 節點 * @項目名稱 java_datastruct * @包名 com.struct.linklist * @類名 node * @author chenlin * @date 2010年6月26日 上午7:58:59 * @version 1.0 */ public class node { public long data; public node next; public long getdata() { return data; } public void display(){ system.out.print(data + " " ); } public node( long data) { this .data = data; } public void setdata( long data) { this .data = data; } public node getnext() { return next; } public void setnext(node next) { this .next = next; } } |
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/** * @描述 鏈表 * @項目名稱 java_datastruct * @包名 com.struct.linklist * @類名 linklist * @author chenlin * @date 2010年6月26日 上午8:00:28 * @version 1.0 */ public class linklist { private node first; public linklist(){ first = null ; } /** * 插入數據 * @param value */ public void insertfirst( long value){ node newnode = new node(value); if (first == null ) { first = newnode; } else { //把first節點往下移動 newnode.next = first; //把插入的節點作為新的節點 first = newnode; } } /** * 刪除頭節點 * @param value * @return */ public node deletefirst(){ if (first == null ) { throw new runtimeexception( "鏈表數據不存在" ); } node temp = first; first = temp.next; return temp; } public node deletebykey( long key){ node current = first; node last = first; while (current.data != key){ if (current.next == null ) { system.out.println( "沒找到節點" ); return null ; } last = current; current = current.next; } if (current == first) { //return deletefirst(); //指向下個就表示刪除第一個 first = first.next; } else { last.next = current.next; } return current; } /** * 顯示所有的數據 */ public void display(){ if (first == null ) { //throw new runtimeexception("鏈表數據不存在"); return ; } node current = first; while (current != null ){ current.display(); current = current.next; } system.out.println( "---------------" ); } /** * 查找節點1 * @param value * @return */ public node findbyvalue( long value){ node current = first; while (current != null ){ if (current.data != value) { current = current.next; } else { break ; } } if (current == null ) { system.out.println( "沒找到" ); return null ; } return current; } /** * 查找節點2 * * @param key * @return */ public node findbykey( long key) { node current = first; while (current.data != key) { if (current.next == null ) { system.out.println( "沒找到" ); return null ; } current = current.next; } return current; } /** * 根據索引查找對應的值 * @param position * @return */ public node findbyposition( int position){ node current = first; //為什么是position - 1,因為要使用遍歷,讓current指向下一個, 所以position - 1的下個node就是要找的值 for ( int i = 0 ; i < position - 1 ; i++) { current = current.next; } return current; } public static void main(string[] args) { linklist linklist = new linklist(); linklist.insertfirst( 21 ); linklist.insertfirst( 22 ); linklist.insertfirst( 23 ); linklist.insertfirst( 24 ); linklist.insertfirst( 25 ); linklist.insertfirst( 26 ); linklist.insertfirst( 27 ); system.out.println( "服務器之家測試結果:" ); linklist.display(); system.out.println( "---查找-------------------------------------" ); linklist.findbykey( 25 ).display(); system.out.println( "--刪除first-------------------------------------" ); //linklist.deletefirst().display(); ///linklist.deletefirst().display(); //linklist.deletefirst().display(); //linklist.deletefirst().display(); system.out.println( "-刪除指定值---------------------------------------" ); linklist.deletebykey( 27 ).display(); linklist.deletebykey( 21 ).display(); system.out.println( "----------------------------------------" ); linklist.display(); } } |
顯示結果:
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/lovoo/article/details/51674479