forked from JoeKarlsson/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.js
297 lines (258 loc) · 6.75 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
class Node {
/**
* Creates an instance of Node.
*
* @param {any} value Node's value
* @param {Node} [next] The next Node in the list
*/
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}
/**
* Javascript list implementation
*
* @export
* @class List
*/
class List {
/**
* Creates an instance of List.
*
* @param {any} initialValue Value to initiate the list with.
*/
constructor(initialValue) {
if (initialValue) {
this.head = new Node(initialValue);
}
}
/**
* Computes the length of the list
*
* @return {number} The length of the list.
*/
getLength() {
let current = this.head;
let length = 0;
while (current) {
current = current.next;
length += 1;
}
return length;
}
/**
* Returns the head of the list (the first element)
*
* @return {Node} The head node (the first node in the list)
*/
getHead() {
return this.head;
}
/**
* Returns the tail of the list (the last element)
*
* @return {Node} The tail node (the last node in the list)
*/
getTail() {
return this.reduce((_, node) => node, undefined, false);
}
/**
* @callback reduceCallbackFn
* @param {any} accumulated
* @param {any} current
* @return {any}
*/
/**
* Reduces the list to a single value
*
* @param {reduceCallbackFN} callbackFn Callback which reduces the list.
* @param {any} [startingValue] Value to initiate the reducing with.
* @param {boolean} [extractValues=true] Decides on what will be passed to the callbackFn,
* either values or whole nodes.
* @return {any} Reduced value
*/
reduce(callbackFn, startingValue, extractValues = true) {
let currentNode;
let accumulated;
let extractorFn;
if (extractValues) {
extractorFn = node => node.value;
} else {
extractorFn = node => node;
}
if (!this.head) {
return startingValue;
}
if (startingValue === undefined) {
currentNode = this.head;
accumulated = startingValue;
} else {
currentNode = this.head.next;
accumulated = extractorFn(this.head);
}
while (currentNode) {
accumulated = callbackFn(accumulated, extractorFn(currentNode));
currentNode = currentNode.next;
}
return accumulated;
}
/**
* @callback valueCallbackFn
* @param {any} value
* @param {number} index
*/
/**
* Traverses the list and executes the callback function for each element in the list.
* The first argument of the callback function is the node's value, the second one is the index.
*
* @param {valueCallbackFn} callbackFn Function invoked for each element in the list.
*/
forEach(callbackFn) {
let index = 0;
this.reduce((_, value) => {
callbackFn(value, index);
index += 1;
return null;
});
}
/**
* Executes callbackFn on each item from the list and returns the results as another list.
*
* @param {valueCallbackFn} callbackFn Function invoked for each element in the list
* @return {List} Transformed list
*/
map(callbackFn) {
const outputList = new List();
this.forEach((value, index) => {
const transformedValue = callbackFn(value, index);
outputList.pushBack(transformedValue);
});
return outputList;
}
/**
* Retrieves the Node with a given index
*
* @param {number} targetIndex Wanted node's index
* @return {Node} Found node
*/
get(targetIndex) {
let currentIndex = 0;
let currentNode = this.head;
if (targetIndex < 0 ) {
throw new Error('Index exceeds list\'s size');
}
while (currentIndex < targetIndex && currentNode) {
if (currentNode.next === null) {
throw new Error('Index exceeds list\'s size');
}
currentIndex += 1;
currentNode = currentNode.next;
}
return currentNode;
}
/**
* Adds a value to the end of the list
*
* @param {any} newValue A value to be added
* @return {List} This list. Allows for chainability
*/
pushBack(newValue) {
const newNode = new Node(newValue);
const tail = this.getTail();
if (tail) {
tail.next = newNode;
} else {
this.head = newNode;
}
return this;
}
/**
* Adds a value at a given index
*
* @param {any} value Value to be added.
* @param {number} index Index at which the value should be added.
* @throws {Error} Index must not exceed list size.
* @return {List} This list. Allows for chainability.
*/
push(value, index) {
if (index === 0) {
// Replace head
this.head = new Node(value, this.head);
} else {
const previousNode = this.get(index - 1);
if (!previousNode) {
throw new Error('Index exceeds list\'s size');
}
const nextNode = previousNode.next;
previousNode.next = new Node(value, nextNode);
}
return this.head;
}
/**
* Removes nodes with specific values from the list
*
* @param {any} valueToRemove A value to be removed
* @return {List} This list. Allows for chainability.
*/
remove(valueToRemove) {
if (this.head.next === null) {
this.head = {};
return this.head;
}
if (this.head.value === valueToRemove) {
this.head = this.head.next;
return this.remove(valueToRemove);
}
this._removeRecursive(this.head, valueToRemove);
return this;
}
/**
* Used internally by remove. Replaces the current node with a next one if the value matches.
*
* @private
* @param {Node} previousNode Reference to the previous node.
* @param {any} valueToRemove Value to be removed.
*/
_removeRecursive(previousNode, valueToRemove) {
if (!previousNode.next) {
return;
}
const currentNode = previousNode.next;
if (currentNode.value === valueToRemove) {
previousNode.next = currentNode.next;
this._removeRecursive(previousNode, valueToRemove);
} else {
this._removeRecursive(currentNode, valueToRemove);
}
}
/**
* Returns the first node that value matches.
*
* @param {any} value Value to be found
* @return {Node} Node with that value
*/
find(value) {
const findRecursive = (node, value) => {
if (!node) {
return null;
} else if (node.value === value) {
return node;
}
return findRecursive(node.next, value);
};
return findRecursive(this.head, value);
}
/**
* Converts the list to an array
*
* @return {any[]} Array of values from the list
*/
getValues() {
const valuesArray = [];
this.forEach(value => valuesArray.push(value));
return valuesArray;
}
}
module.exports = List;
// Source: https://github.com/Gelio/js-list