-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathlinkedlist.js
374 lines (343 loc) · 11.7 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
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
/**
* Creates an empty Linked List.
* @class A linked list is a sequence of items arranged one after
* another. The size is not fixed and it can grow or shrink
* on demand. One of the main benefits of a linked list is that
* you can add or remove elements at both ends in constant time.
* One disadvantage of a linked list against an array is
* that it doesn’t provide constant time random access.
* @constructor
*/
buckets.LinkedList = function () {
/**
* @exports list as buckets.LinkedList
* @private
*/
var list = {},
// Number of elements in the list
nElements = 0,
// First node in the list
firstNode,
// Last node in the list
lastNode;
// Returns the node at the specified index.
function nodeAtIndex(index) {
var node, i;
if (index < 0 || index >= nElements) {
return undefined;
}
if (index === (nElements - 1)) {
return lastNode;
}
node = firstNode;
for (i = 0; i < index; i += 1) {
node = node.next;
}
return node;
}
/**
* Adds an element to the list.
* @param {Object} item Element to be added.
* @param {number=} index Optional index to add the element. If no index is specified
* the element is added to the end of the list.
* @return {boolean} True if the element was added or false if the index is invalid
* or if the element is undefined.
*/
list.add = function (item, index) {
var newNode, prev;
if (buckets.isUndefined(index)) {
index = nElements;
}
if (index < 0 || index > nElements || buckets.isUndefined(item)) {
return false;
}
newNode = {
element: item,
next: undefined
};
if (nElements === 0) {
// First node in the list.
firstNode = newNode;
lastNode = newNode;
} else if (index === nElements) {
// Insert at the end.
lastNode.next = newNode;
lastNode = newNode;
} else if (index === 0) {
// Change first node.
newNode.next = firstNode;
firstNode = newNode;
} else {
prev = nodeAtIndex(index - 1);
newNode.next = prev.next;
prev.next = newNode;
}
nElements += 1;
return true;
};
/**
* Returns the first element in the list.
* @return {*} The first element in the list or undefined if the list is
* empty.
*/
list.first = function () {
if (firstNode !== undefined) {
return firstNode.element;
}
return undefined;
};
/**
* Returns the last element in the list.
* @return {*} The last element in the list or undefined if the list is
* empty.
*/
list.last = function () {
if (lastNode !== undefined) {
return lastNode.element;
}
return undefined;
};
/**
* Returns the element at the specified position in the list.
* @param {number} index Desired index.
* @return {*} The element at the given index or undefined if the index is
* out of bounds.
*/
list.elementAtIndex = function (index) {
var node = nodeAtIndex(index);
if (node === undefined) {
return undefined;
}
return node.element;
};
/**
* Returns the index of the first occurrence of the
* specified element, or -1 if the list does not contain the element.
* <p>If the elements inside the list are
* not comparable with the === operator, a custom equals function should be
* provided to perform searches, that function must receive two arguments and
* return true if they are equal, false otherwise. Example:</p>
*
* <pre>
* var petsAreEqualByName = function(pet1, pet2) {
* return pet1.name === pet2.name;
* }
* </pre>
* @param {Object} item Element to search for.
* @param {function(Object,Object):boolean=} equalsFunction Optional
* function used to check if two elements are equal.
* @return {number} The index in the list of the first occurrence
* of the specified element, or -1 if the list does not contain the
* element.
*/
list.indexOf = function (item, equalsFunction) {
var equalsF = equalsFunction || buckets.defaultEquals,
currentNode = firstNode,
index = 0;
if (buckets.isUndefined(item)) {
return -1;
}
while (currentNode !== undefined) {
if (equalsF(currentNode.element, item)) {
return index;
}
index += 1;
currentNode = currentNode.next;
}
return -1;
};
/**
* Returns true if the list contains the specified element.
* <p>If the elements inside the list are
* not comparable with the === operator, a custom equals function should be
* provided to perform searches, that function must receive two arguments and
* return true if they are equal, false otherwise. Example:</p>
*
* <pre>
* var petsAreEqualByName = function(pet1, pet2) {
* return pet1.name === pet2.name;
* }
* </pre>
* @param {Object} item Element to search for.
* @param {function(Object,Object):boolean=} equalsFunction Optional
* function used to check if two elements are equal.
* @return {boolean} True if the list contains the specified element, false
* otherwise.
*/
list.contains = function (item, equalsFunction) {
return (list.indexOf(item, equalsFunction) >= 0);
};
/**
* Removes the first occurrence of the specified element in the list.
* <p>If the elements inside the list are
* not comparable with the === operator, a custom equals function should be
* provided to perform searches, that function must receive two arguments and
* return true if they are equal, false otherwise. Example:</p>
* <pre>
* var petsAreEqualByName = function(pet1, pet2) {
* return pet1.name === pet2.name;
* }
* </pre>
* @param {Object} item Element to be removed from the list, if present.
* @return {boolean} True if the list contained the specified element.
*/
list.remove = function (item, equalsFunction) {
var equalsF = equalsFunction || buckets.defaultEquals,
currentNode = firstNode,
previous;
if (nElements < 1 || buckets.isUndefined(item)) {
return false;
}
while (currentNode !== undefined) {
if (equalsF(currentNode.element, item)) {
if (currentNode === firstNode) {
firstNode = firstNode.next;
if (currentNode === lastNode) {
lastNode = undefined;
}
} else if (currentNode === lastNode) {
lastNode = previous;
previous.next = currentNode.next;
currentNode.next = undefined;
} else {
previous.next = currentNode.next;
currentNode.next = undefined;
}
nElements = nElements - 1;
return true;
}
previous = currentNode;
currentNode = currentNode.next;
}
return false;
};
/**
* Removes all the elements from the list.
*/
list.clear = function () {
firstNode = undefined;
lastNode = undefined;
nElements = 0;
};
/**
* Returns true if the list is equal to another list.
* Two lists are equal if they have the same elements in the same order.
* @param {buckets.LinkedList} other The other list.
* @param {function(Object,Object):boolean=} equalsFunction Optional
* function to check if two elements are equal. If the elements in the lists
* are custom objects you should provide a custom equals function, otherwise
* the === operator is used to check equality between elements.
* @return {boolean} true if the list is equal to the given list.
*/
list.equals = function (other, equalsFunction) {
var eqf = equalsFunction || buckets.defaultEquals,
isEqual = true,
node = firstNode;
if (buckets.isUndefined(other) || typeof other.elementAtIndex !== 'function') {
return false;
}
if (list.size() !== other.size()) {
return false;
}
other.forEach(function (element) {
isEqual = eqf(element, node.element);
node = node.next;
return isEqual;
});
return isEqual;
};
/**
* Removes the element at the specified position in the list.
* @param {number} index Given index.
* @return {*} Removed element or undefined if the index is out of bounds.
*/
list.removeElementAtIndex = function (index) {
var element, previous;
if (index < 0 || index >= nElements) {
return undefined;
}
if (nElements === 1) {
//First node in the list.
element = firstNode.element;
firstNode = undefined;
lastNode = undefined;
} else {
previous = nodeAtIndex(index - 1);
if (previous === undefined) {
element = firstNode.element;
firstNode = firstNode.next;
} else if (previous.next === lastNode) {
element = lastNode.element;
lastNode = previous;
}
if (previous !== undefined) {
element = previous.next.element;
previous.next = previous.next.next;
}
}
nElements -= 1;
return element;
};
/**
* Executes the provided function once per element present in the list in order.
* @param {function(Object):*} callback Function to execute, it is
* invoked with one argument: the element value, to break the iteration you can
* optionally return false inside the callback.
*/
list.forEach = function (callback) {
var currentNode = firstNode;
while (currentNode !== undefined) {
if (callback(currentNode.element) === false) {
break;
}
currentNode = currentNode.next;
}
};
/**
* Reverses the order of the elements in the linked list (makes the last
* element first, and the first element last).
* @memberOf buckets.LinkedList
*/
list.reverse = function () {
var current = firstNode,
previous,
temp;
while (current !== undefined) {
temp = current.next;
current.next = previous;
previous = current;
current = temp;
}
temp = firstNode;
firstNode = lastNode;
lastNode = temp;
};
/**
* Returns an array containing all the elements in the list in proper
* sequence.
* @return {Array.<*>} An array containing all the elements in the list,
* in proper sequence.
*/
list.toArray = function () {
var result = [];
list.forEach(function (element) {
result.push(element);
});
return result;
};
/**
* Returns the number of elements in the list.
* @return {number} The number of elements in the list.
*/
list.size = function () {
return nElements;
};
/**
* Returns true if the list contains no elements.
* @return {boolean} true if the list contains no elements.
*/
list.isEmpty = function () {
return nElements <= 0;
};
return list;
};