-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
linked-list.js
261 lines (217 loc) · 4.62 KB
/
linked-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
/**
* Mnemonist Linked List
* ======================
*
* Singly linked list implementation. Uses raw JavaScript objects as nodes
* as benchmarks proved it was the fastest thing to do.
*/
var Iterator = require('obliterator/iterator'),
forEach = require('obliterator/foreach');
/**
* Linked List.
*
* @constructor
*/
function LinkedList() {
this.clear();
}
/**
* Method used to clear the list.
*
* @return {undefined}
*/
LinkedList.prototype.clear = function() {
// Properties
this.head = null;
this.tail = null;
this.size = 0;
};
/**
* Method used to get the first item of the list.
*
* @return {any}
*/
LinkedList.prototype.first = function() {
return this.head ? this.head.item : undefined;
};
LinkedList.prototype.peek = LinkedList.prototype.first;
/**
* Method used to get the last item of the list.
*
* @return {any}
*/
LinkedList.prototype.last = function() {
return this.tail ? this.tail.item : undefined;
};
/**
* Method used to add an item at the end of the list.
*
* @param {any} item - The item to add.
* @return {number}
*/
LinkedList.prototype.push = function(item) {
var node = {item: item, next: null};
if (!this.head) {
this.head = node;
this.tail = node;
}
else {
this.tail.next = node;
this.tail = node;
}
this.size++;
return this.size;
};
/**
* Method used to add an item at the beginning of the list.
*
* @param {any} item - The item to add.
* @return {number}
*/
LinkedList.prototype.unshift = function(item) {
var node = {item: item, next: null};
if (!this.head) {
this.head = node;
this.tail = node;
}
else {
if (!this.head.next)
this.tail = this.head;
node.next = this.head;
this.head = node;
}
this.size++;
return this.size;
};
/**
* Method used to retrieve & remove the first item of the list.
*
* @return {any}
*/
LinkedList.prototype.shift = function() {
if (!this.size)
return undefined;
var node = this.head;
this.head = node.next;
this.size--;
return node.item;
};
/**
* Method used to iterate over the list.
*
* @param {function} callback - Function to call for each item.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
LinkedList.prototype.forEach = function(callback, scope) {
if (!this.size)
return;
scope = arguments.length > 1 ? scope : this;
var n = this.head,
i = 0;
while (n) {
callback.call(scope, n.item, i, this);
n = n.next;
i++;
}
};
/**
* Method used to convert the list into an array.
*
* @return {array}
*/
LinkedList.prototype.toArray = function() {
if (!this.size)
return [];
var array = new Array(this.size);
for (var i = 0, l = this.size, n = this.head; i < l; i++) {
array[i] = n.item;
n = n.next;
}
return array;
};
/**
* Method used to create an iterator over a list's values.
*
* @return {Iterator}
*/
LinkedList.prototype.values = function() {
var n = this.head;
return new Iterator(function() {
if (!n)
return {
done: true
};
var value = n.item;
n = n.next;
return {
value: value,
done: false
};
});
};
/**
* Method used to create an iterator over a list's entries.
*
* @return {Iterator}
*/
LinkedList.prototype.entries = function() {
var n = this.head,
i = 0;
return new Iterator(function() {
if (!n)
return {
done: true
};
var value = n.item;
n = n.next;
i++;
return {
value: [i - 1, value],
done: false
};
});
};
/**
* Attaching the #.values method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
LinkedList.prototype[Symbol.iterator] = LinkedList.prototype.values;
/**
* Convenience known methods.
*/
LinkedList.prototype.toString = function() {
return this.toArray().join(',');
};
LinkedList.prototype.toJSON = function() {
return this.toArray();
};
LinkedList.prototype.inspect = function() {
var array = this.toArray();
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: LinkedList,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
LinkedList.prototype[Symbol.for('nodejs.util.inspect.custom')] = LinkedList.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a list.
*
* @param {Iterable} iterable - Target iterable.
* @return {LinkedList}
*/
LinkedList.from = function(iterable) {
var list = new LinkedList();
forEach(iterable, function(value) {
list.push(value);
});
return list;
};
/**
* Exporting.
*/
module.exports = LinkedList;