-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.js
236 lines (190 loc) · 5.42 KB
/
LinkedList.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
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.position = 0;
}
}
class LinkedList {
constructor(value) {
this.head = null;
this.tail = null;
this.length = 0;
this.initialize(value);
}
// Initializing linkedList
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`
);
const newNode = new Node(value);
if (this.tail) {
this.tail.next = newNode;
newNode.position = this.tail.position + 1;
}
this.tail = newNode;
if (!this.head) this.head = newNode;
this.updatePosition();
}
prepend(value) {
if (!value.toString().trim(""))
throw new Error(
`Object.prepend requires at least 1 argument, but only 0 were passed`
);
const newNode = new Node(value);
if (!this.head) this.head = this.tail = newNode;
else {
newNode.next = this.head;
this.head = newNode;
}
this.updatePosition();
}
insertAfter(value, afterValue) {
if (!this.head) throw new Error(`Linkedlist is Empty!`);
let currentNode = this.find(afterValue);
if (currentNode) {
const newNode = new Node(value);
newNode.next = currentNode.next;
currentNode.next = newNode;
this.updatePosition();
}
}
remove(value) {
if (!this.head) throw new Error(`Linkedlist is Empty!`);
if (!value.toString().trim(""))
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;
let currentNode = this.head;
while (currentNode.next) {
if (currentNode.next.value === value)
currentNode.next = currentNode.next.next;
else currentNode = currentNode.next;
}
this.updatePosition();
}
pop() {
if (!this.head) throw new Error(`Linkedlist is Empty!`);
if (this.head === this.tail) this.clear();
let currentNode = this.head;
while (currentNode) {
if (!currentNode.next.next) currentNode.next = null;
this.tail = currentNode;
currentNode = currentNode.next;
}
return undefined;
}
shift() {
if (!this.head) throw new Error(`Linkedlist is Empty!`);
if (this.head === this.tail) this.clear();
this.head = this.head.next;
this.updatePosition();
}
reverse() {
if (!this.head) throw new Error(`Linkedlist 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;
prev = currentNode;
currentNode = next;
}
this.updatePosition();
}
find(value) {
if (!this.head) throw new Error(`Linkedlist is Empty!`);
let currentNode = this.head;
while (currentNode) {
if (currentNode.value === value) return currentNode;
currentNode = currentNode.next;
}
return undefined;
}
set(curretValue, newValue) {
let currentNode = this.find(curretValue);
if (currentNode) currentNode.value = newValue;
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;
}
}
includes(value) {
if (!value.toString().trim(""))
throw new Error(
`Object.includes requires at least 1 argument, but only 0 were passed`
);
return this.find(value) ? true : false;
}
updatePosition() {
let initialPosition = 0;
let currentNode = this.head;
while (currentNode) {
currentNode.position = initialPosition;
initialPosition++;
currentNode = currentNode.next;
}
this.length = this.tail.position + 1;
}
toArray() {
if (!this.head) throw new Error("LinkedList is Empty!");
const elements = [];
let currentNode = this.head;
while (currentNode) {
const obj = {
value: currentNode.value,
next: currentNode.next,
position: currentNode.position,
};
elements.push(obj);
currentNode = currentNode.next;
}
return elements;
}
values() {
if (!this.head) throw new Error("LinkedList is Empty!");
return this.toArray().map((i) => i.value);
}
clear() {
this.head = this.tail = null;
this.length = 0;
}
}
const linkedList = new LinkedList("Hello, There");
linkedList.append(1);
linkedList.append(2);
linkedList.append(3);
linkedList.prepend("Dinesh");
linkedList.prepend("Hello");
linkedList.insertAfter("Is this Works!", "Hello");
linkedList.reverse();
linkedList.pop();
linkedList.shift();
linkedList.set(2, "great");
console.log(linkedList);
console.log(linkedList.toArray());