forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 5
/
linkedList.js
270 lines (236 loc) · 5.48 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
class Node {
// Constructs Node
constructor (data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
// Initialises Linked List
constructor (size = 0) {
this.head = null;
this.length = size;
}
// Inserts Element with value at index
add (value, index) {
let temp = this.head;
if (index === 0) {
let n = new Node(value);
n.next = this.head;
this.head = n;
} else if (index < 0 || index >= this.length) {
throw new Error('Index Out of Bound');
} else {
for (let i = 0; i < index - 1; i++) {
temp = temp.next;
}
let n = new Node(value);
n.next = temp.next;
temp.next = n;
}
this.length++;
}
// Adds value at front of Linked List
addFirst (value) {
return this.add(value, 0);
}
// Adds value at end of Linked List
addLast (value) {
if (this.head === null) {
this.addFirst(value);
} else {
let temp = this.head;
while (temp.next !== null) {
temp = temp.next;
}
let n = new Node(value);
temp.next = n;
this.length++;
}
}
// Remove value from index in Linked List
remove (index) {
if (this.isEmpty()) {
throw new Error('List is Empty');
} else if (index < 0 || index >= this.length) {
throw new Error('Index out of Bound');
} else if (index === 0) {
this.head = this.head.next;
} else {
let temp = this.head;
if (temp.next == null) {
this.head = null;
} else {
for (let i = 0; i < index - 1; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
}
}
this.length--;
}
// Removes front element from Linked List
removeFront () {
this.remove(0);
}
// Removes last element from Linked List
removeLast () {
this.remove(this.length - 1);
}
// Searches and removes element with data = value.
removeByValue (value) {
this.remove(this.findFirst(value));
}
// Search and returns index.
search (input) {
let head = this.head;
for (let i = 0; i < this.length; i++) {
if (head.data === input) {
return i;
} else {
head = head.next;
}
}
throw new Error('Value not found');
}
// Search and return index of first occurence of input.
findFirst (input) {
return this.search(input);
}
// Search and return index of last occurence of input.
findLast (input) {
let newList = new LinkedList();
newList = this.clone();
newList.reverse();
return this.length - newList.search(input) - 1;
}
// Sets Data of input at index.
setData (input, index) {
// If index is >= size of Linked List, It adds the value.
if (index >= this.length) {
this.add(input, index);
} else {
// If index already have some data it replaces it with input.
let head = this.head;
for (let i = 0; i < this.length; i++) {
if (i === index) {
head.data = input;
}
head = head.next;
}
}
}
// Clones and returns a newList.
clone () {
let newList = new LinkedList();
let temp = this.head;
while (temp.next) {
newList.addLast(temp.data);
temp = temp.next;
}
newList.addLast(temp.data);
return newList;
}
// Reverses the existing Linked List.
reverse () {
let prev = null;
let current = this.head;
while (current) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
}
// Checks if Linked List is empty.
isEmpty () {
return (this.length === 0);
}
// Clears the Linked List.
clear () {
this.head = null;
this.length = 0;
}
// Returns the Linked List in List form.
show () {
let list = [];
let head = this.head;
while (head) {
list.push(head.data);
head = head.next;
}
return list;
}
}
function main () {
let linkedList = new LinkedList();
linkedList.add(5, 0);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
linkedList.addFirst(25);
linkedList.addFirst(251);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
linkedList.addFirst(5);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
try {
console.log('Fist Occurrence at index: ' + linkedList.findFirst(5));
} catch (err) {
console.log('Value not found');
}
try {
console.log('Last Occurrence at index: ' + linkedList.findLast(5));
} catch (err) {
console.log('Value not found');
}
try {
linkedList.setData(12325, 1);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
} catch (err) {
console.log('Index out of bound');
}
try {
linkedList.removeByValue(12325);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
} catch (err) {
console.log('Value not found');
}
linkedList.addLast(125);
linkedList.addLast(11);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
try {
console.log('Element found at index: ' + linkedList.search(1125));
} catch (err) {
console.log('Value not found');
}
console.log('Cloned : ', linkedList.clone().show());
try {
linkedList.remove(6);
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
} catch (err) {
console.log('Index Out of Bound');
}
try {
linkedList.remove(2);
} catch (err) {
console.log('Index Out of Bound');
}
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
linkedList.removeFront();
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
linkedList.removeLast();
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
linkedList.clear();
console.log(linkedList.show());
console.log('Size : ' + linkedList.length);
}
main();