/
linked_list.js
130 lines (106 loc) · 3.29 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
(function(factory) {
var E = (typeof exports === 'object'),
js = (typeof JS === 'undefined') ? require('./core') : JS,
Enumerable = js.Enumerable || require('./enumerable').Enumerable;
if (E) exports.JS = exports;
factory(js, Enumerable, E ? exports : js);
})(function(JS, Enumerable, exports) {
'use strict';
var LinkedList = new JS.Class('LinkedList', {
include: Enumerable || {},
initialize: function(array, useNodes) {
this.length = 0;
this.first = this.last = null;
if (!array) return;
for (var i = 0, n = array.length; i < n; i++)
this.push( useNodes ? new this.klass.Node(array[i]) : array[i] );
},
forEach: function(block, context) {
if (!block) return this.enumFor('forEach');
block = Enumerable.toFn(block);
var node = this.first,
next, i, n;
for (i = 0, n = this.length; i < n; i++) {
next = node.next;
block.call(context, node, i);
if (node === this.last) break;
node = next;
}
return this;
},
at: function(n) {
if (n < 0 || n >= this.length) return undefined;
var node = this.first;
while (n--) node = node.next;
return node;
},
pop: function() {
return this.length ? this.remove(this.last) : undefined;
},
shift: function() {
return this.length ? this.remove(this.first) : undefined;
},
// stubs - should be implemented by concrete list types
insertAfter: function() {},
push: function() {},
remove: function() {},
extend: {
Node: new JS.Class({
initialize: function(data) {
this.data = data;
this.prev = this.next = this.list = null;
}
})
}
});
LinkedList.Doubly = new JS.Class('LinkedList.Doubly', LinkedList, {
insertAt: function(n, newNode) {
if (n < 0 || n >= this.length) return;
this.insertBefore(this.at(n), newNode);
},
unshift: function(newNode) {
this.length > 0
? this.insertBefore(this.first, newNode)
: this.push(newNode);
},
insertBefore: function() {}
});
LinkedList.insertTemplate = function(prev, next, pos) {
return function(node, newNode) {
if (node.list !== this) return;
newNode[prev] = node;
newNode[next] = node[next];
node[next] = (node[next][prev] = newNode);
if (newNode[prev] === this[pos]) this[pos] = newNode;
newNode.list = this;
this.length++;
};
};
LinkedList.Doubly.Circular = new JS.Class('LinkedList.Doubly.Circular', LinkedList.Doubly, {
insertAfter: LinkedList.insertTemplate('prev', 'next', 'last'),
insertBefore: LinkedList.insertTemplate('next', 'prev', 'first'),
push: function(newNode) {
if (this.length)
return this.insertAfter(this.last, newNode);
this.first = this.last =
newNode.prev = newNode.next = newNode;
newNode.list = this;
this.length = 1;
},
remove: function(removed) {
if (removed.list !== this || this.length === 0) return null;
if (this.length > 1) {
removed.prev.next = removed.next;
removed.next.prev = removed.prev;
if (removed === this.first) this.first = removed.next;
if (removed === this.last) this.last = removed.prev;
} else {
this.first = this.last = null;
}
removed.prev = removed.next = removed.list = null;
this.length--;
return removed;
}
});
exports.LinkedList = LinkedList;
});