-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
390 lines (313 loc) · 6.66 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
function linkedlist () {
//Node
let Node = function (elm) {
this.element = elm
this.next = null
}
//To keep track of the size
let length = 0
//To keep track of the list
let head = null
//Other methods go here
//Add new item in the linkedlist
this.append = function (elm) {
let node = new Node(elm),
current
if (head === null) {
head = node
} else {
current = head
while (current.next) {
current = current.next
}
current.next = node
}
length++
}
//Remove item from any position
this.removeAt = function (pos) {
if (pos > -1 && pos < length) {
let current = head,
previous,
index = 0
if (pos === 0) {
head = current.next
} else {
while (index++ < pos) {
previous = current
current = current.next
}
previous.next = current.next
}
length--
return current.element
} else {
return null
}
}
//Add item at any position
this.insert = function (pos, elm) {
if (pos >= 0 && pos <= length) {
let node = new Node(elm),
current = head,
previous,
index = 0
if (pos === 0) {
node.next = current
head = node
} else {
while (index++ < pos) {
previous = current
current = current.next
}
node.next = current
previous.next = node
}
length++
return true
} else {
return false
}
}
//Print item of the string
this.toString = function () {
let current = head,
string = ''
while (current) {
string += current.element + (current.next ? '\n' : '')
current = current.next
}
return string
}
//Convert list to array
this.toArray = function () {
let arr = [],
current = head
while (current) {
arr.push(current.element)
current = current.next
}
return arr
}
//Get the indexOf item
this.indexOf = function (elm) {
let current = head,
index = -1
while (current) {
if (elm === current.element) {
return ++index
}
index++
current = current.next
}
return -1
}
//Delete an item from the list
this.delete = elm => {
return this.removeAt(this.indexOf(elm))
}
//Delete first item from the list
this.deleteHead = function () {
let current = head
if (current === null) {
return true
}
if (current.next) {
current = current.next
head = current
} else {
head = null
}
return true
}
//Delete last item from the list
this.deleteTail = function () {
let current = head
if (current === null) {
return true
}
while (current.next) {
if (!current.next.next) {
current.next = null
} else {
current = current.next
}
}
return true
}
//Find the item in the list
this.isPresent = elm => {
return this.indexOf(elm) !== -1
}
//Check if list is empty
this.isEmpty = function () {
return length === 0
}
//Get the size of the list
this.size = function () {
return length
}
//Get the head of the list
this.getHead = function () {
return head
}
}
/*********************************************************
* CLASS IMPLEMENTATION *
*********************************************************/
//Node
class Node {
constructor (elm, next = null) {
this.element = elm
this.next = next
}
}
//LinkedList
class LinkedList {
constructor () {
this.length = 0
this.head = null
}
//Add new item in the linkedlist
append = function (elm) {
let node = new Node(elm),
current
if (this.head === null) {
this.head = node
} else {
current = this.head
while (current.next) {
current = current.next
}
current.next = node
}
this.length++
}
//Remove item from any position
removeAt = function (pos) {
if (pos > -1 && pos < this.length) {
let current = this.head,
previous,
index = 0
if (pos === 0) {
this.head = current.next
} else {
while (index++ < pos) {
previous = current
current = current.next
}
previous.next = current.next
}
this.length--
return current.element
} else {
return null
}
}
//Add item at any position
insert = function (pos, elm) {
if (pos >= 0 && pos <= this.length) {
let node = new Node(elm),
current = this.head,
previous,
index = 0
if (pos === 0) {
node.next = current
this.head = node
} else {
while (index++ < pos) {
previous = current
current = current.next
}
node.next = current
previous.next = node
}
this.length++
return true
} else {
return false
}
}
//Print item of the string
toString = function () {
let current = this.head,
string = ''
while (current) {
string += current.element + (current.next ? '\n' : '')
current = current.next
}
return string
}
//Convert list to array
toArray = function () {
let arr = [],
current = this.head
while (current) {
arr.push(current.element)
current = current.next
}
return arr
}
//Get the indexOf item
indexOf = function (elm) {
let current = this.head,
index = -1
while (current) {
if (elm === current.element) {
return ++index
}
index++
current = current.next
}
return -1
}
//Delete an item from the list
delete = elm => {
return this.removeAt(this.indexOf(elm))
}
//Delete first item from the list
deleteHead = function () {
let current = this.head
if (current === null) {
return true
}
if (current.next) {
current = current.next
this.head = current
} else {
this.head = null
}
return true
}
//Delete last item from the list
deleteTail = function () {
let current = this.head
if (current === null) {
return true
}
while (current.next) {
if (!current.next.next) {
current.next = null
} else {
current = current.next
}
}
return true
}
//Find the item in the list
isPresent = elm => {
return this.indexOf(elm) !== -1
}
//Check if list is empty
isEmpty = function () {
return this.length === 0
}
//Get the size of the list
size = function () {
return this.length
}
//Get the head of the list
getHead = function () {
return this.head
}
}