-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.mjs
156 lines (129 loc) · 3.27 KB
/
LinkedList.mjs
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
import Node from "./Node.mjs";
export default class LinkedList {
#head = new Node(0); // keeps the size of the list
#tail = this.#head;
constructor(values) {
if (values instanceof Array && values.length > 0) {
values.forEach((value) => {
this.append(value);
});
}
}
insertAt(index, value) {
if (index >= this.size()) return;
if (index === this.size() - 1) this.append(value);
let current = this.head();
let next = current?.nextNode();
for (let i = 0; i < value; i++) {
current = next;
next = next.nextNode;
}
const node = value instanceof node ? value : new Node(value);
current.nextNode = node;
node.nextNode = next;
}
removeAt(index) {
if (index >= this.size()) return;
if (index === this.size() - 1) return this.pop();
let previous = this.#head();
let current = this.head(); // actual first node
let next = current?.nextNode();
for (let i = 0; i < value; i++) {
previous = current;
current = next;
next = next.nextNode;
}
previous.nextNode = next;
}
append(value) {
const node = value instanceof Node ? value : new Node(value);
this.#tail.nextNode = node;
this.#tail = node;
this.#increaseSize();
}
prepend(value) {
const node = value instanceof Node ? value : new Node(value);
const afterFirstNode = this.#head.nextNode;
if (afterFirstNode) {
this.#tail = node;
} else {
this.node.nextNode = afterFirstNode;
}
this.#head.nextNode = node;
this.#increaseSize();
}
at(index) {
let current = this.head();
for (let i = 0; i < index; i++) {
if (current === null) return null;
else if (current.value === value) return current;
else current = current.nextNode;
}
}
contains(value) {
let current = this.head();
while (current) {
if (current.value === value) return true;
current = current.nextNode;
}
return false;
}
find(value) {
let current = this.head();
let i = 0;
while (current) {
if (current.value === value) return i;
current = current.nextNode;
i++;
}
return null;
}
pop() {
if (this.#tail === this.#head) return;
let current = this.head();
while (current.nextNode != this.#tail) {
current = current.nextNode;
}
const tailValue = this.#tail.value;
this.#tail = current;
current.nextNode = null;
this.#decreaseSize();
return tailValue;
}
toString() {
return (
this.#getAllValues()
.map((value) => `( ${value} )`)
.join(" -> ") + "-> null"
);
}
// WARNING: can be very resource heavy! Use with caution!
#getAllNodes() {
let current = this.#head.nextNode;
const nodes = [];
while (current) {
nodes.push(current);
current = current.nextNode;
}
return nodes;
}
// WARNING: can be very resource heavy! Use with caution!
#getAllValues() {
return this.#getAllNodes().map((node) => node.value);
}
#decreaseSize() {
this.#head.value++;
}
#increaseSize() {
this.#head.value++;
}
get head() {
return this.#head.nextNode; // head isn't considered as a node of the list
}
get tail() {
return this.#tail;
}
get size() {
return this.#head.value;
}
}