forked from Wizcorp/list-CircularDoubly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·159 lines (136 loc) · 3.48 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
/**
* CIRCULAR 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 CircularDoublyList() {
this.count = 0;
this.last = null;
this.first = null;
}
CircularDoublyList.prototype.add = function (obj) {
this.count += 1;
var newNode = new Node(obj, this.last, this.first);
if (this.first === null) {
this.first = newNode;
this.last = newNode;
newNode.previous = newNode;
newNode.next = newNode;
} else {
// insertion before the this.last one
this.last.next = newNode;
this.last = newNode;
this.first.previous = newNode;
}
return newNode;
};
CircularDoublyList.prototype.remove = function (obj) {
if (this.first === this.last) {
if (this.first.object === obj) {
this.first = null;
this.last = null;
this.count -= 1;
}
return;
}
var current = this.first;
while (current !== this.last) {
if (current.object === obj) {
// Removing any reference to the node
current.next.previous = current.previous;
if (current === this.first) {
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;
return;
}
current = current.next;
}
if (current.object === obj) {
// Removing any reference to the node
this.last = current.previous;
if (current === this.first) {
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;
return;
}
};
CircularDoublyList.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;
};
CircularDoublyList.prototype.getFirst = function () {
return this.first;
};
CircularDoublyList.prototype.getLast = function () {
return this.last;
};
CircularDoublyList.prototype.clear = function () {
this.first = null;
this.last = null;
};
CircularDoublyList.prototype.getCount = function () {
return this.count;
};
CircularDoublyList.prototype.forEach = function (processingFunc, params) {
for (var current = this.first; current !== this.last; current = current.next) {
processingFunc(current.object, params);
}
if (current !== null) {
processingFunc(current.object, params);
}
};
CircularDoublyList.prototype.forEachReverse = function (processingFunc, params) {
for (var current = this.last; current !== this.first; current = current.next) {
processingFunc(current.object, params);
}
if (current !== null) {
processingFunc(current.object, params);
}
};
module.exports = CircularDoublyList;