-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.js
More file actions
236 lines (194 loc) · 5.55 KB
/
Copy pathDoublyLinkedList.js
File metadata and controls
236 lines (194 loc) · 5.55 KB
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
class Node {
constructor(value) {
this.value = value;
this.prev = null;
this.next = null;
this.position = 0;
}
}
class DoublyLinkedList {
constructor(value) {
this.head = null;
this.tail = null;
this.length = 0;
this.initialize(value);
}
initialize(value) {
if (value) this.head = this.tail = new Node(value);
this.length++;
}
append(value) {
if (!value.toString().trim(""))
throw new Error(
`Object.append requires at least 1 argument, but only 0 were passed`
);
let newNode = new Node(value);
if (this.tail) {
newNode.prev = this.tail;
this.tail.next = newNode;
newNode.position = this.tail.position + 1;
}
this.tail = newNode;
if (!this.head) this.head = newNode;
}
prepend(value) {
if (!value.toString().trim(""))
throw new Error(
`Object.prepend requires at least 1 argument, but only 0 were passed`
);
if (!this.head) return (this.head = this.tail = newNode);
const newNode = new Node(value);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
this.updatePosition();
}
insertAfter(value, currentValue) {
const currentNode = this.find(currentValue);
if (currentNode) {
const newNode = new Node(value);
newNode.next = currentNode.next;
currentNode.next = newNode;
newNode.prev = currentNode;
} else return undefined;
}
remove(value) {
if (!this.head) throw new Error(`DoublyLinkedList is Empty!`);
if (!value)
throw new Error(
`Object.remove requires at least 1 argument, but only 0 were passed`
);
while (this.head && this.head.value === value) {
this.head = this.head.next;
this.head.prev = null;
}
let currentNode = this.head;
while (currentNode.next) {
if (currentNode.next.value === value) {
currentNode.next = currentNode.next.next;
currentNode.next.prev = currentNode;
} else currentNode = currentNode.next;
}
this.updatePosition();
}
set(newValue, currentValue) {
let currentNode = this.find(currentValue);
if (currentNode) {
currentNode.value = newValue;
currentNode = this.find(currentValue);
if (currentNode) this.set(newValue, currentValue);
} else return undefined;
}
fill(value, startValue = null, endValue = null) {
let currentNode = this.head;
let isLoopStarted = false;
let isStartValueInList = null;
let isEndValueInList = null;
if (startValue && endValue) {
isStartValueInList = this.find(startValue);
isEndValueInList = this.find(endValue);
}
while (currentNode) {
if (!startValue && !endValue && value) currentNode.value = value;
else if (isStartValueInList && isEndValueInList) {
if (currentNode.value === startValue) isLoopStarted = true;
if (currentNode.value === endValue) isLoopStarted = false;
if (isLoopStarted) currentNode.value = value;
}
currentNode = currentNode.next;
}
}
reverse() {
if (!this.head) throw new Error(`DoublyLinkedList is Empty!`);
let currentNode = this.head;
this.head = this.tail;
this.tail = currentNode;
let prev = null;
let next = currentNode.next;
while (next) {
next = currentNode.next;
currentNode.next = prev;
currentNode.prev = next;
prev = currentNode;
currentNode = next;
}
this.updatePosition();
}
indexAt(index) {
return this.toArray().find((i) => i.position == index);
}
pop() {
if (!this.head) throw new Error(`DoublyLinkedList is Empty!`);
if (this.tail === this.head) return this.clear();
this.tail = this.tail.prev;
this.tail.next = null;
}
shift() {
if (!this.head) throw new Error(`DoublyLinkedList is Empty!`);
if (this.tail === this.head) return this.clear();
this.head = this.head.next;
this.head.prev = null;
this.updatePosition();
}
find(value) {
if (!value.toString().trim(""))
throw new Error(
`Object.find requires at least 1 argument, but only 0 were passed`
);
let currentNode = this.head;
while (currentNode) {
if (currentNode.value === value) return currentNode;
currentNode = currentNode.next;
}
return undefined;
}
values() {
return this.toArray().map((i) => i.value);
}
includes(value) {
return this.find(value) ? true : false;
}
toArray() {
const elements = [];
let currentNode = this.head;
while (currentNode) {
const obj = {
value: currentNode.value,
prev: currentNode.prev,
next: currentNode.next,
position: currentNode.position,
};
elements.push(obj);
currentNode = currentNode.next;
}
return elements;
}
updatePosition() {
let initialPosition = 0;
let currentNode = this.head;
while (currentNode) {
currentNode.position = initialPosition;
initialPosition++;
currentNode = currentNode.next;
}
this.length = this.tail.position + 1;
}
clear() {
this.head = null;
this.tail = null;
this.length = 0;
}
}
const doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.append(1);
doublyLinkedList.prepend(0);
Array.from({ length: 5 }, (_, i) => doublyLinkedList.append(i));
doublyLinkedList.pop();
doublyLinkedList.shift();
doublyLinkedList.set("Dinesh", 1);
doublyLinkedList.find("Dinesh");
doublyLinkedList.includes(1);
doublyLinkedList.insertAfter("Hello", 2);
doublyLinkedList.reverse();
doublyLinkedList.remove(2);
console.log(doublyLinkedList.toArray());