-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·193 lines (168 loc) · 4.05 KB
/
index.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
/**
* DOUBLY LIST Class
*
* @author Brice Chevalier
*
* @desc Doubly list data structure
*
* Method Time Complexity
* ___________________________________
*
* add O(1)
* remove O(n)
* removeByRef O(1)
* getFirst O(1)
* getLast O(1)
* getCount O(1)
* apply O(n)
* clear O(n)
*
* Memory Complexity in O(n)
*/
function Node(obj, previous, next) {
this.object = obj;
this.previous = previous;
this.next = next;
}
function DoublyList() {
this.count = 0;
this.last = null;
this.first = null;
}
DoublyList.prototype.add = function (obj) {
this.count += 1;
var newNode = new Node(obj, null, this.first);
if (this.first === null) {
this.first = newNode;
this.last = newNode;
} else {
// insertion before the this.first one
this.first.previous = newNode;
this.first = newNode;
}
return newNode;
};
DoublyList.prototype.pushBack = function (obj) {
this.count += 1;
var newNode = new Node(obj, this.last, null);
if (this.first === null) {
this.first = newNode;
this.last = newNode;
} else {
// insertion after the this.last one
this.last.next = newNode;
this.last = newNode;
}
return newNode;
};
DoublyList.prototype.addBefore = function (obj, nodeRef) {
this.count += 1;
var newNode = new Node(obj, nodeRef.previous, nodeRef);
if (nodeRef.previous !== null) {
nodeRef.previous.next = newNode;
} else {
this.first = newNode;
}
nodeRef.previous = newNode;
return newNode;
};
DoublyList.prototype.addAfter = function (obj, nodeRef) {
this.count += 1;
var newNode = new Node(obj, nodeRef, nodeRef.next);
if (nodeRef.next !== null) {
nodeRef.next.previous = newNode;
} else {
this.last = newNode;
}
nodeRef.next = newNode;
return newNode;
};
DoublyList.prototype.replace = function (obj, nodeRef) {
nodeRef.obj = obj;
return nodeRef;
};
DoublyList.prototype.remove = function (obj) {
var current = this.first;
while (current !== null) {
if (current.object === obj) {
// Removing any reference to the node
if (current.next === null) {
this.last = current.previous;
} else {
current.next.previous = current.previous;
}
if (current.previous === null) {
this.first = current.next;
} else {
current.previous.next = current.next;
}
// Removing any reference from the node to any other element of the list
current.previous = null;
current.next = null;
this.count -= 1;
break;
}
current = current.next;
}
};
DoublyList.prototype.getRef = function (obj) {
var current = this.first;
while (current !== null) {
if (current.object === obj) {
return current;
}
current = current.next;
}
return null;
};
DoublyList.prototype.removeByRef = function (node) {
// Removing any reference to the node
if (node.next === null) {
this.last = node.previous;
} else {
node.next.previous = node.previous;
}
if (node.previous === null) {
this.first = node.next;
} else {
node.previous.next = node.next;
}
// Removing any reference from the node to any other element of the list
node.previous = null;
node.next = null;
this.count -= 1;
};
DoublyList.prototype.getRef = function (obj) {
var current = this.first;
while (current !== null) {
if (current.object === obj) {
return current;
}
current = current.next;
}
return null;
};
DoublyList.prototype.getFirst = function () {
return this.first;
};
DoublyList.prototype.getLast = function () {
return this.last;
};
DoublyList.prototype.clear = function () {
this.first = null;
this.last = null;
};
DoublyList.prototype.getCount = function () {
return this.count;
};
DoublyList.prototype.forEach = function (processingFunc, params) {
for (var current = this.first; current; current = current.next) {
processingFunc(current.object, params);
}
};
DoublyList.prototype.forEachReverse = function (processingFunc, params) {
for (var current = this.last; current; current = current.previous) {
processingFunc(current.object, params);
}
};
module.exports = DoublyList;