-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.js
317 lines (264 loc) · 7.68 KB
/
LinkedList.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
import LinkedListNode from "./LinkedListNode.js";
import Comparator from "../../utils/comparator.js";
export default class LinkedList {
/**
* Builds a linked list with custom comparator function or default
* comparator if none is given.
* @param {Function} customComparator Initialize this linked list with a
* custom comparator function
*/
constructor(customComparator = null) {
/** @var LinkedListNode */
this.head = null;
/** @var LinkedListNode */
this.tail = null;
/** @var Number */
this.size = 0;
this.comparator = new Comparator(customComparator);
}
length() {
return this.size;
}
/**
* Appends a new node to this linked list.
* @param {any} value Value to append
* @returns {LinkedList} This linked list
*/
append(value) {
const node = new LinkedListNode(value);
this.size++;
// The new node is the head if this list is empty
if (!this.head) {
this.head = node;
this.tail = node;
return this;
}
// Attach the new node to the end of the linked list
this.tail.next = node;
this.tail = node;
return this;
}
/**
* Prepends the given value to this linked list making it the new head.
* @param {any} value The value to prepend to the list. New head.
* @returns {LinkedList} This linked list
*/
prepend(value) {
// The new node has to be the head of this list no matter what
const node = new LinkedListNode(value, this.head);
this.head = node;
// If there is no tail yet, we must have to create it
if (!this.tail) {
this.tail = node;
}
this.size++;
return this;
}
/**
*
* @param {any} value The value to insert in this linked list
* @param {number} rawIndex The position where the value must be inserted at
* @returns {LinkedList} This linked list
*/
insert(value, rawIndex) {
const index = rawIndex < 0 ? 0 : rawIndex;
if (index === 0) this.prepend(value);
else {
this.size++;
let count = 1;
let currentNode = this.head;
const node = new LinkedListNode(value);
while (currentNode) {
if (count === index) break;
currentNode = currentNode.next;
count++;
}
if (currentNode) {
node.next = currentNode.next;
currentNode.next = node;
} else {
if (this.tail) {
this.tail.next = node;
this.tail = node;
} else {
this.head = node;
this.tail = node;
}
}
}
return this;
}
/**
* Deletes and returns the first node that match with the value out of this
* linked list. Returns null if the linked list is empty.
* @param {any} value Value to delete and return
* @returns {(LinkedListNode|null)} The deleted node from this linked list
*/
delete(value) {
if (!this.head) return null;
let deletedNode = null;
let currentNode = this.head;
if (this.head.value === value) {
deletedNode = this.head;
this.head = deletedNode.next;
this.size--;
return deletedNode;
}
while (currentNode.next) {
if (this.comparator.equal(currentNode.next.value, value)) break;
currentNode = currentNode.next;
}
deletedNode = currentNode.next;
if (!deletedNode) return deletedNode;
if (this.comparator.equal(deletedNode.value, this.tail.value))
this.tail = currentNode;
currentNode.next = deletedNode.next;
this.size--;
return deletedNode;
}
/**
* Deletes all the nodes that have the specified value out of this linked list.
*
* Returns a single deleted node, or null if the linked list is empty.
*
* @param {any} value Value to be deleted from this linked list
* @returns {(LinkedList|null)} This linked list without nodes having the
* specified value
*/
deleteAll(value) {
if (!this.head) return null;
let deletedNode = null;
while (this.head && this.comparator.equal(this.head.value, value)) {
deletedNode = this.head;
this.head = deletedNode.next;
this.size--;
}
let currentNode = this.head;
if (currentNode !== null)
while (currentNode.next) {
if (this.comparator.equal(currentNode.next.value, value)) {
deletedNode = currentNode.next;
currentNode.next = deletedNode.next;
this.size--;
} else {
currentNode = currentNode.next;
}
}
if (this.comparator.equal(this.tail.value, value)) this.tail = currentNode;
return deletedNode;
}
/**
* Deletes the tail, updating this linked list new tail and returning the
* deleted node. Returns null if the linked list is empty.
* @returns {(LinkedListNode|null)} The tail node of this linked list if
* it exists
*/
deleteTail() {
if (!this.head) return null;
let deletedNode = null;
if (!this.head.next) {
deletedNode = this.head;
this.head = null;
this.tail = null;
return deletedNode;
}
let currentNode = this.head;
while (currentNode.next.next) {
currentNode = currentNode.next;
}
deletedNode = currentNode.next;
currentNode.next = null;
this.tail = currentNode;
this.size--;
return deletedNode;
}
/**
* Deletes this linked list head and returns it if it exists. Updates
* references to
* new head and tail if applies.
* @returns {(LinkedListNode|null)}
*/
deleteHead() {
if (!this.head) return null;
let deletedNode = this.head;
if (this.head === this.tail) this.tail = null;
this.head = deletedNode.next;
this.size--;
return deletedNode;
}
/**
* Finds a node by either its value or a custom search function.
* Returns null if there is no matches for the given finding parameters.
* @param {Object} findParams Finding object
* @param {any} findParams.value Find by value
* @param {Function} findParams.callback Find by function
* @returns {(LinkedListNode|null)} Node that matches finding criteria
*/
find({ value = undefined, callback = undefined }) {
if (!this.head) return null;
let currentNode = this.head;
while (currentNode) {
if (callback && callback(currentNode.value)) return currentNode;
if (
value !== undefined &&
this.comparator.equal(currentNode.value, value)
)
return currentNode;
currentNode = currentNode.next;
}
return null;
}
/**
* Populates this linked list from a given array.
* @param {any[]} array Array to process
*/
fromArray(array) {
for (const e of array) {
this.append(e);
}
}
/**
* Represent this linked list as an array of its nodes.
* @returns {LinkedListNode[]} Array of nodes in this linked list
*/
toArray() {
const nodes = [];
let currentNode = this.head;
while (currentNode) {
nodes.push(currentNode);
currentNode = currentNode.next;
}
return nodes;
}
/**
* Reverse a linked list.
* @returns {LinkedList}
*/
reverse() {
let currNode = this.head;
let prevNode = null;
let nextNode = null;
while (currNode) {
// Store next node.
nextNode = currNode.next;
// Change next node of the current node so it would link to previous node.
currNode.next = prevNode;
// Move prevNode and currNode nodes one step forward.
prevNode = currNode;
currNode = nextNode;
}
// Reset head and tail.
this.tail = this.head;
this.head = prevNode;
return this;
}
/**
* Returns a string representing this linked list separated by commas
* @returns {string} This linked list string representation
*/
toString(stringifierFn) {
return this.toArray()
.map((n) => n.toString(stringifierFn))
.toString();
}
}