-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathList.js
435 lines (387 loc) · 10 KB
/
List.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* A list lets you add and remove elements at the current position
* of the iterator, which starts at the start of the list and can be moved with the functions.
*/
/**
* The List Node Class
* @private
*/
class Node {
/**
* The List Node Constructor
* @constructor
* @param {*} data
* @param {Node} prev
* @param {Node} next
*/
constructor(data, prev, next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
/**
* The List Iterator Class
* @private
*/
export class Iterator {
/**
* The List Iterator Constructor
* @constructor
* @param {List} list
* @param {Node} prev
* @param {Node} next
*/
constructor(list, prev, next) {
this.list = list;
this.previows = prev;
this.following = next;
}
/**
* Moves to the next element if there is one
* @returns {Void}
*/
next() {
if (this.hasNext()) {
this.previows = this.following;
this.following = this.following.next;
}
}
/**
* Moves to the previews element if there is one
* @returns {Void}
*/
prev() {
if (this.hasPrev()) {
this.following = this.previows;
this.previows = this.previows.prev;
}
}
/**
* Checks if there is a next elements (from the current one)
* @returns {Boolean}
*/
hasNext() {
return this.following !== null;
}
/**
* Checks if there is a previews element (from the current one)
* @returns {Boolean}
*/
hasPrev() {
return this.previows !== null;
}
/**
* Returns the following elements data
* @returns {*}
*/
getNext() {
if (this.hasNext()) {
return this.following.data;
}
}
/**
* Returns the previws elements data
* @returns {*}
*/
getPrev() {
if (this.hasPrev()) {
return this.previows.data;
}
}
/**
* Adds an item at the current position
* @param {*} item
* @returns {Void}
*/
add(item) {
const node = new Node(item, this.previows, this.following);
if (this.previows) {
this.previows.next = node;
} else {
this.list.head = node;
}
if (this.following) {
this.following.prev = node;
} else {
this.list.tail = node;
}
this.list.length += 1;
}
/**
* Removes the follwing element and sets the next one as the new following element
* @returns {Void}
*/
removeNext() {
// Cant remove next if there isn't one
if (!this.hasNext()) {
return;
}
if (this.following.next) {
this.following.next.prev = this.following.prev;
} else {
this.list.tail = this.following.prev;
}
if (this.following.prev) {
this.following.prev.next = this.following.next;
} else {
this.list.head = this.following.next;
}
this.following = this.following.next;
this.list.length -= 1;
}
/**
* Removes the previows element and sets the prev one as the new previows element
* @returns {Void}
*/
removePrev() {
if (this.hasPrev()) {
this.prev();
this.removeNext();
}
}
}
/**
* The List Class
*/
export default class List {
/**
* The List Constructor
* @constructor
* @param {Array=} array
*/
constructor(array) {
this.head = null;
this.tail = null;
this.length = 0;
if (array) {
for (const item of array) {
this.addLast(item);
}
}
}
/**
* Adds the element between the previows and following
* @private
* @param {*} item
* @param {Node} prev
* @param {Node} next
* @returns {Node}
*/
add(item, prev, next) {
const node = new Node(item, prev, next);
if (this.head === null) {
this.head = node;
this.tail = node;
} else if (prev) {
this.tail.next = node;
this.tail = node;
} else if (next) {
this.head.prev = node;
this.head = node;
}
this.length += 1;
return node;
}
/**
* Adds the item at the beggining of the list
* @param {*} item
* @returns {Iterator}
*/
addFirst(item) {
this.add(item, null, this.head);
return this.iterate();
}
/**
* Adds the item at the end of the list
* @param {*} item
* @returns {Iterator}
*/
addLast(item) {
this.add(item, this.tail, null);
return this.iterateLast();
}
/**
* Iterates throught the list calling the callback with the data as parameter,
* and adds the item if the callback returns true
* @param {*} item
* @param {Function} callback
* @returns {Boolean} True if the element was added
*/
addBefore(item, callback) {
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
if (callback(it.getNext(), count)) {
it.add(item);
return true;
}
}
}
return false;
}
/**
* Empties the List
* @returns {Void}
*/
empty() {
if (this.head) {
for (let it = this.iterate(); it.hasNext(); it.next()) {
it.removeNext();
}
}
this.head = null;
this.tail = null;
this.length = 0;
}
/**
* Iterates throught the list calling the callback with the data as parameter,
* and remove the item if the callback returns true
* @param {Function} callback
* @returns {Boolean} True if the element was removed
*/
remove(callback) {
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
if (callback(it.getNext(), count)) {
it.removeNext();
return true;
}
}
}
return false;
}
/**
* Returns the data from the first element
* @returns {*}
*/
get first() {
if (this.head) {
return this.head.data;
}
return null;
}
/**
* Returns the data from the last element
* @returns {*}
*/
get last() {
if (this.tail) {
return this.tail.data;
}
return null;
}
/**
* Returns the size of the List
* @returns {Number}
*/
get size() {
return this.length;
}
/**
* Returns true if the List is empty, and false otherwise
* @returns {Boolean}
*/
get isEmpty() {
return this.head === null;
}
/**
* Creates and returns a new Iterator at the start of the list
* @returns {Iterator}
*/
iterate() {
if (this.head) {
return new Iterator(this, null, this.head);
}
return null;
}
/**
* Creates and returns a new Iterator at the end of the list
* @returns {Iterator}
*/
iterateLast() {
if (this.tail) {
return new Iterator(this, this.tail, null);
}
return null;
}
/**
* Iterates throught the list calling the callback with the data as parameter
* @param {Function} callback
* @returns {Void}
*/
forEach(callback) {
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
callback(it.getNext(), count);
}
}
}
/**
* Iterates throught the list calling the callback with the data as parameter,
* but it breaks the loop if the function returns true
* @param {Function} callback
* @returns {Boolean}
*/
some(callback) {
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
if (callback(it.getNext(), count)) {
return true;
}
}
}
return false;
}
/**
* Iterates throught the list calling the callback with the data as parameter,
* and returns the item if the callback returns true
* @param {Function} callback
* @returns {?*} The found element or null
*/
find(callback) {
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
if (callback(it.getNext(), count)) {
return it.getNext();
}
}
}
return null;
}
/**
* Iterates throught the list calling the callback with the data as parameter,
* and returns the item if the callback returns true
* @param {Function} callback
* @returns {Array} The found elements
*/
findAll(callback) {
const result = [];
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
if (callback(it.getNext(), count)) {
result.push(it.getNext());
}
}
}
return result;
}
/**
* Iterates throught the list calling the callback with the data as parameter,
* and creates an array as a result
* @param {Function=} callback
* @returns {Array} The array result
*/
toArray(callback = null) {
const result = [];
if (this.head) {
for (let it = this.iterate(), count = 0; it.hasNext(); it.next(), count += 1) {
if (callback) {
result.push(callback(it.getNext(), count));
} else {
result.push(it.getNext());
}
}
}
return result;
}
}