-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list.js
More file actions
54 lines (45 loc) · 1.04 KB
/
Copy pathlinked-list.js
File metadata and controls
54 lines (45 loc) · 1.04 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
//
// This is only a SKELETON file for the 'Linked List' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
export class LinkedList {
list = new Array(10);
tracer = -1;
push(element) {
this.list[++this.tracer] = element;
}
pop() {
return this.list[this.tracer--];
}
shift() {
let res = this.list[0];
this.#realloc(false);
return res;
}
unshift(element) {
this.#realloc(true);
this.list[0] = element;
}
#realloc(isAdd, terminatingIndex) {
terminatingIndex = terminatingIndex || 0;
if (isAdd) {
for (let i = this.list.length; i > terminatingIndex; i--) {
this.list[i] = this.list[i - 1];
}
this.tracer++;
} else {
for (let i = terminatingIndex; i < this.list.length - 1; i++) {
this.list[i] = this.list[i + 1];
}
this.tracer--;
}
}
delete(element) {
let indexDeletionTakePlace = this.list.findIndex(el => el === element);
if (indexDeletionTakePlace != -1)
this.#realloc(false, indexDeletionTakePlace);
}
count() {
return this.tracer + 1;
}
}