forked from jasonsjones/doubly-linked-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
522 lines (442 loc) · 16.4 KB
/
index.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/**
* @fileOverview Implementation of a doubly linked-list data structure
* @author Jason S. Jones
* @license MIT
*/
(function () {
'use strict';
var isEqual = require('lodash.isequal');
var Node = require('./lib/list-node');
var Iterator = require('./lib/iterator');
/**************************************************
* Doubly linked list class
*
* Implementation of a doubly linked list data structure. This
* implementation provides the general functionality of adding nodes to
* the front or back of the list, as well as removing node from the front
* or back. This functionality enables this implemention to be the
* underlying data structure for the more specific stack or queue data
* structure.
*
***************************************************/
/**
* Creates a LinkedList instance. Each instance has a head node, a tail
* node and a size, which represents the number of nodes in the list.
*
* @constructor
*/
function DoublyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
this.nodeHashTable = {};
// add iterator as a property of this list to share the same
// iterator instance with all other methods that may require
// its use. Note: be sure to call this.iterator.reset() to
// reset this iterator to point the head of the list.
this.iterator = new Iterator(this);
}
/* Functions attached to the Linked-list prototype. All linked-list
* instances will share these methods, meaning there will NOT be copies
* made for each instance. This will be a huge memory savings since there
* may be several different linked lists.
*/
DoublyLinkedList.prototype = {
/**
* Creates a new Node object with 'data' assigned to the node's data
* property
*
* @param {object|string|number} data The data to initialize with the
* node
* @returns {object} Node object intialized with 'data'
*/
createNewNode: function (data, id) {
var node = new Node(data, id);
this.nodeHashTable[node.id] = node;
return node;
},
/**
* Returns the first node in the list, commonly referred to as the
* 'head' node
*
* @returns {object} the head node of the list
*/
getHeadNode: function () {
return this.head;
},
/**
* Returns the last node in the list, commonly referred to as the
* 'tail'node
*
* @returns {object} the tail node of the list
*/
getTailNode: function () {
return this.tail;
},
/**
* Determines if the list is empty
*
* @returns {boolean} true if the list is empty, false otherwise
*/
isEmpty: function () {
return (this.size === 0);
},
/**
* Returns the size of the list, or number of nodes
*
* @returns {number} the number of nodes in the list
*/
getSize: function () {
return this.size;
},
/**
* Clears the list of all nodes/data
*/
clear: function () {
while (!this.isEmpty()) {
this.remove();
}
},
//################## INSERT methods ####################
/**
* Inserts a node with the provided data to the end of the list
*
* @param {object|string|number} data The data to initialize with the
* node
* @returns {boolean} true if insert operation was successful
*/
insert: function (data, id) {
var newNode = this.createNewNode(data, id);
if (this.isEmpty()) {
this.head = this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.size += 1;
return true;
},
/**
* Inserts a node with the provided data to the front of the list
*
* @param {object|string|number} data The data to initialize with the
* node
* @returns {boolean} true if insert operation was successful
*/
insertFirst: function (data, id) {
if (this.isEmpty()) {
this.insert(data, id);
} else {
var newNode = this.createNewNode(data, id);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
this.size += 1;
}
return true;
},
/**
* Inserts a node with the provided data at the index indicated.
*
* @param {number} index The index in the list to insert the new node
* @param {object|string|number} data The data to initialize with the node
*/
insertAt: function (index, data, id) {
var current = this.getHeadNode(),
newNode = this.createNewNode(data, id),
position = 0;
// check for index out-of-bounds
if (index < 0 || index > this.getSize() - 1) {
return false;
}
// if index is 0, we just need to insert the first node
if (index === 0) {
this.insertFirst(data);
return true;
}
while (position < index) {
current = current.next;
position += 1;
}
current.prev.next = newNode;
newNode.prev = current.prev;
current.prev = newNode;
newNode.next = current;
this.size += 1;
return true;
},
/**
* Inserts a node before the first node containing the provided data
*
* @param {object|string|number} nodeData The data of the node to
* find to insert the new node before
* @param {object|string|number} dataToInsert The data to initialize with the node
* @returns {boolean} true if insert operation was successful
*/
insertBefore: function (nodeData, dataToInsert) {
var index = this.indexOf(nodeData);
return this.insertAt(index, dataToInsert);
},
/**
* Inserts a node after the first node containing the provided data
*
* @param {object|string|number} nodeData The data of the node to
* find to insert the new node after
* @param {object|string|number} dataToInsert The data to initialize with the node
* @returns {boolean} true if insert operation was successful
*/
insertAfter: function (nodeData, dataToInsert) {
var index = this.indexOf(nodeData);
var size = this.getSize();
// check if we want to insert new node after the tail node
if (index + 1 === size) {
// if so, call insert, which will append to the end by default
return this.insert(dataToInsert);
} else {
// otherwise, increment the index and insert there
return this.insertAt(index + 1, dataToInsert);
}
},
//################## REMOVE methods ####################
/**
* Removes the tail node from the list
*
* There is a significant performance improvement with the operation
* over its singly linked list counterpart. The mere fact of having
* a reference to the previous node improves this operation from O(n)
* (in the case of singly linked list) to O(1).
*
* @returns the node that was removed
*/
remove: function () {
if (this.isEmpty()) {
return null;
}
// get handle for the tail node
var nodeToRemove = this.getTailNode();
// if there is only one node in the list, set head and tail
// properties to null
if (this.getSize() === 1) {
this.head = null;
this.tail = null;
// more than one node in the list
} else {
this.tail = this.getTailNode().prev;
this.tail.next = null;
}
this.size -= 1;
delete this.nodeHashTable[nodeToRemove.id];
return nodeToRemove;
},
/**
* Removes the head node from the list
*
* @returns the node that was removed
*/
removeFirst: function () {
if (this.isEmpty()) {
return null;
}
var nodeToRemove;
if (this.getSize() === 1) {
nodeToRemove = this.remove();
} else {
nodeToRemove = this.getHeadNode();
this.head = this.head.next;
this.head.prev = null;
this.size -= 1;
}
delete this.nodeHashTable[nodeToRemove.id];
return nodeToRemove;
},
/**
* Removes the node at the index provided
*
* @param {number} index The index of the node to remove
* @returns the node that was removed
*/
removeAt: function (index) {
var nodeToRemove = this.findAt(index);
// check for index out-of-bounds
if (index < 0 || index > this.getSize() - 1) {
return null;
}
// if index is 0, we just need to remove the first node
if (index === 0) {
return this.removeFirst();
}
// if index is size-1, we just need to remove the last node,
// which remove() does by default
if (index === this.getSize() - 1) {
return this.remove();
}
nodeToRemove.prev.next = nodeToRemove.next;
nodeToRemove.next.prev = nodeToRemove.prev;
nodeToRemove.next = nodeToRemove.prev = null;
this.size -= 1;
delete this.nodeHashTable[nodeToRemove.id];
return nodeToRemove;
},
/**
* Removes the first node that contains the data provided
*
* @param {object|string|number} nodeData The data of the node to remove
* @returns the node that was removed
*/
removeNode: function (nodeData) {
var index = this.indexOf(nodeData);
return this.removeAt(index);
},
/**
* Removes a node found by the passed id
*
* @param {string|number} nodeData The data of the node to remove
* @returns {Node|number} the node that was removed, -1 if not found
*/
removeNodeById: function (id) {
var nodeToRemove = this.findById(id);
if (nodeToRemove === -1)
return -1;
if (nodeToRemove.id === this.getHeadNode().id) {
return this.removeFirst();
}
if (nodeToRemove.id === this.getTailNode().id){
return this.remove();
}
nodeToRemove.prev.next = nodeToRemove.next;
nodeToRemove.next.prev = nodeToRemove.prev;
nodeToRemove.next = nodeToRemove.prev = null;
this.size -= 1;
delete this.nodeHashTable[nodeToRemove.id];
return nodeToRemove;
},
//################## FIND methods ####################
/**
* Returns the index of the first node containing the provided data. If
* a node cannot be found containing the provided data, -1 is returned.
*
* @param {object|string|number} nodeData The data of the node to find
* @returns the index of the node if found, -1 otherwise
*/
indexOf: function (nodeData) {
this.iterator.reset();
var current;
var index = 0;
// iterate over the list (keeping track of the index value) until
// we find the node containg the nodeData we are looking for
while (this.iterator.hasNext()) {
current = this.iterator.next();
if (isEqual(current.getData(), nodeData)) {
return index;
}
index += 1;
}
// only get here if we didn't find a node containing the nodeData
return -1;
},
/**
* finds a node in O(1) time by it's ID indexed by nodeHashTable
*
* @param {string|number} nodeData The id of the node to find
* @returns the node if found, -1 otherwise
*/
findById: function (id){
return this.nodeHashTable[id] || -1;
},
/**
* Returns the fist node containing the provided data. If a node
* cannot be found containing the provided data, -1 is returned.
*
* @param {object|string|number} nodeData The data of the node to find
* @returns the node if found, -1 otherwise
*/
find: function (nodeData) {
// start at the head of the list
this.iterator.reset();
var current;
// iterate over the list until we find the node containing the data
// we are looking for
while (this.iterator.hasNext()) {
current = this.iterator.next();
if (isEqual(current.getData(), nodeData)) {
return current;
}
}
// only get here if we didn't find a node containing the nodeData
return -1;
},
/**
* Returns the node at the location provided by index
*
* @param {number} index The index of the node to return
* @returns the node located at the index provided.
*/
findAt: function (index) {
// if idx is out of bounds or fn called on empty list, return -1
if (this.isEmpty() || index > this.getSize() - 1) {
return -1;
}
// else, loop through the list and return the node in the
// position provided by idx. Assume zero-based positions.
var node = this.getHeadNode();
var position = 0;
while (position < index) {
node = node.next;
position += 1;
}
return node;
},
/**
* Determines whether or not the list contains the provided nodeData
*
* @param {object|string|number} nodeData The data to check if the list
* contains
* @returns the true if the list contains nodeData, false otherwise
*/
contains: function (nodeData) {
if (this.indexOf(nodeData) > -1) {
return true;
} else {
return false;
}
},
//################## UTILITY methods ####################
/**
* Utility function to iterate over the list and call the fn provided
* on each node, or element, of the list
*
* @param {object} fn The function to call on each node of the list
* @param {bool} reverse Use or not reverse iteration (tail to head), default to false
*/
forEach: function (fn, reverse) {
reverse = reverse || false;
if (reverse) {
this.iterator.reset_reverse();
this.iterator.each_reverse(fn);
} else {
this.iterator.reset();
this.iterator.each(fn);
}
},
/**
* Returns an array of all the data contained in the list
*
* @returns {array} the array of all the data from the list
*/
toArray: function () {
var listArray = [];
this.forEach(function (node) {
listArray.push(node.getData());
});
return listArray;
},
/**
* Interrupts iteration over the list
*/
interruptEnumeration: function () {
this.iterator.interrupt();
}
};
module.exports = DoublyLinkedList;
}());