1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > LinkedList</ title >
6
+ < script type ="text/javascript ">
7
+ function LinkedList ( ) {
8
+ //Node类表示链表中操作的项
9
+ var Node = function ( element ) {
10
+ this . element = element ;
11
+ this . next = null ;
12
+ }
13
+
14
+ var length = 0 ;
15
+ var head = null ;
16
+
17
+ //链表尾部添加一项
18
+ this . append = function ( element ) {
19
+ var node = new Node ( element ) ;
20
+ var current ;
21
+ if ( head == null ) {
22
+ head = node ;
23
+ } else {
24
+ current = head ;
25
+ while ( current . next ) {
26
+ current = current . next ;
27
+ }
28
+ current . next = node ;
29
+ }
30
+ length ++ ;
31
+ } ;
32
+
33
+ //特定位置添加元素
34
+ this . insert = function ( position , element ) {
35
+ var node = new Node ( element ) ;
36
+ var current = head ;
37
+ var previous ;
38
+ var index = 0 ;
39
+
40
+ //检查越界位置
41
+ if ( position >= 0 && position <= length ) {
42
+ if ( position === 0 ) {
43
+ node . next = current ;
44
+ head = node ;
45
+ } else {
46
+ while ( index ++ < position ) {
47
+ previous = current ;
48
+ current = current . next ;
49
+ }
50
+ node . next = current ;
51
+ previous . next = node ;
52
+ }
53
+ length ++ ;
54
+ return true ;
55
+ } else {
56
+ return false ;
57
+ }
58
+ } ;
59
+
60
+ //从链表中移除特定位置一项
61
+ this . removeAt = function ( position ) {
62
+
63
+ if ( position > - 1 && position < length ) { //下标越界检查
64
+ var current = head ;
65
+ var previous ;
66
+ var index = 0 ;
67
+
68
+ if ( position === 0 ) {
69
+ head = current . next ;
70
+ } else {
71
+ while ( index ++ < position ) {
72
+ previous = current ;
73
+ current = current . next ;
74
+ }
75
+ previous . next = current . next ;
76
+ }
77
+ length -- ;
78
+ return current . element ;
79
+ } else {
80
+ return null ;
81
+ }
82
+ } ;
83
+
84
+ //从链表中移除一项
85
+ this . remove = function ( element ) {
86
+ var index = this . indexOf ( element ) ;
87
+ return this . removeAt ( index ) ;
88
+ } ;
89
+
90
+ //返回列表中项的索引
91
+ this . indexOf = function ( element ) {
92
+ var current = head ;
93
+ var index = 0 ;
94
+ while ( current ) {
95
+ if ( element == current . element ) {
96
+ return index ;
97
+ }
98
+ index ++ ;
99
+ current = current . next ;
100
+ }
101
+ return - 1 ;
102
+ } ;
103
+ //判空
104
+ this . isEmpty = function ( ) {
105
+ return length === 0 ;
106
+ } ;
107
+ //链表中的长度
108
+ this . size = function ( ) {
109
+ return length ;
110
+ } ;
111
+
112
+ //返回第一个元素
113
+ this . getHead = function ( ) {
114
+ return head ;
115
+ }
116
+
117
+ this . toString = function ( ) {
118
+ var current = head ;
119
+ var string = '' ;
120
+ while ( current ) {
121
+ string += current . element ;
122
+ current = current . next ;
123
+ }
124
+ return string ;
125
+ } ;
126
+
127
+ this . print = function ( ) {
128
+ console . log ( this . toString ( ) ) ;
129
+ }
130
+ }
131
+
132
+ //双向链表
133
+ function DoublyLinkedList ( ) {
134
+ //操作的数据结构
135
+ function Node ( element ) {
136
+ this . element = element ;
137
+ this . prev = null ;
138
+ this . next = null ;
139
+ }
140
+
141
+ var length = 0 ;
142
+ var head = null ;
143
+ var tail = null ;
144
+
145
+ //在任意位置插入元素
146
+ this . insert = function ( position , element ) {
147
+
148
+ if ( position >= 0 && position <= length ) {
149
+ var node = new Node ( element ) ;
150
+ var current = head ;
151
+ var previous ;
152
+ var index = 0 ;
153
+
154
+ if ( position === 0 ) {
155
+ if ( ! head ) {
156
+ head = node ;
157
+ tail = node ;
158
+ } else {
159
+ node . next = current ;
160
+ current . prev = node ;
161
+ head = node ;
162
+ }
163
+ } else if ( position === length ) {
164
+ current = tail ;
165
+ current . next = node ;
166
+ node . prev = current ;
167
+ tail = node ;
168
+ } else {
169
+ while ( index ++ < position ) {
170
+ previous = current ;
171
+ current = current . next ;
172
+ }
173
+ node . next = current ;
174
+ previous . next = node ;
175
+
176
+ current . prev = node ;
177
+ node . prev = previous ;
178
+ }
179
+
180
+ length ++ ; //更新链表长度
181
+
182
+ return true ;
183
+ } else {
184
+ return false ;
185
+ }
186
+ } ;
187
+
188
+ this . removeAt ( position ) {
189
+
190
+ if ( position > - 1 && position < length ) {
191
+
192
+ var current = head ;
193
+ var previous ;
194
+ var index = 0 ;
195
+
196
+ //移除第一项
197
+ if ( position === 0 ) {
198
+ head = current . next ;
199
+
200
+ //如果只有一项,更新tail
201
+ if ( length === 1 ) {
202
+ tail = null ;
203
+ } else {
204
+ head . prev = null ;
205
+ }
206
+ } else if ( position === length - 1 ) {
207
+ current = tail ;
208
+ tail = current . prev ;
209
+ tail . next = null ;
210
+ } else {
211
+
212
+ while ( index ++ < position ) {
213
+ previous = current ;
214
+ current = current . next ;
215
+ }
216
+
217
+ previous . next = current . next ;
218
+ current . next . prev = previous ;
219
+ }
220
+ length -- ;
221
+ return current . element ;
222
+ } else {
223
+ return null ;
224
+ }
225
+ }
226
+ }
227
+ </ script >
228
+ </ head >
229
+ < body >
230
+
231
+ </ body >
232
+ </ html >
0 commit comments