-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.js
204 lines (185 loc) · 4.71 KB
/
linked-list.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
class Node {
constructor(element) {
this.element = element;
this.next = null;
}
}
export default class LinkedList {
constructor() {
this.head = null; // head node harus selalu merujuk ke node pertama dalam list
this.tail = null;
this.size = 0;
}
isEmpty() {
return this.size === 0;
}
getSize() {
return this.size;
}
prepend(value) {
const newNode = new Node(value);
if (this.isEmpty()) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.size++;
}
append(value) {
const newNode = new Node(value);
if (this.isEmpty()) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.size++;
}
insert(value, position) {
if (!Number.isInteger(position) || position < 0 || position > this.size) {
console.log("Invalid position");
return;
}
if (position === 0) {
this.prepend(value);
} else {
const newNode = new Node(value);
let current = this.head;
let prev = null;
let index = 0;
while (index < position) {
prev = current;
current = prev.next;
index++;
}
prev.next = newNode;
newNode.next = current;
if (position === this.size) {
this.tail = newNode;
}
this.size++;
}
}
removeFrom(position) {
if (!Number.isInteger(position) || position < 0 || position >= this.size) {
return "Invalid position";
}
if (position === 0) {
this.removeFromFront();
} else if (position === this.size - 1) {
this.removeFromEnd();
} else {
let removedNode;
let prev = this.head;
// hasil dari perulangan akan mendapatkan node sebelum node yang akan dihapus dan menyimpannya dalam variabel `prev`
for (let i = 0; i < position - 1; i++) {
prev = prev.next;
}
removedNode = prev.next;
prev.next = removedNode.next;
this.size--;
return removedNode.element;
}
}
removeValue(value) {
if (this.isEmpty()) {
return console.log("List is empty");
}
if (this.head.element === value) {
return this.removeFromFront();
} else if (this.tail.element === value) {
return this.removeFromEnd();
} else {
let prev = this.head;
// memeriksa apakah ada elemen selanjutnya dan nilai elemen selanjutnya tidak sama dengan `value`
while (prev.next && prev.next.element !== value) {
// jika tidak ditemukan elemen dengan nilai yang sama dengan `value`, maka prev.next akan bernilai null
prev = prev.next;
}
// jika prev.next bukan null, berarti elemen dengan nilai `value` ditemukan dalam linked list
if (prev.next) {
const removedNode = prev.next;
prev.next = removedNode.next;
this.size--;
return value;
}
return null;
}
}
removeFromFront() {
if (this.isEmpty()) {
return console.log("List is empty");
}
const value = this.head.element;
this.head = this.head.next;
if (this.head === null) {
this.tail = null;
}
this.size--;
return value;
}
removeFromEnd() {
if (this.isEmpty()) {
return console.log("List is empty");
}
const value = this.tail.element;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else {
let prev = this.head;
while (prev.next !== this.tail) {
prev = prev.next;
}
prev.next = null;
this.tail = prev;
}
this.size--;
return value;
}
search(value) {
if (this.isEmpty()) {
return -1;
}
let index = 0;
let current = this.head;
while (current) {
if (current.element === value) {
return index;
}
current = current.next;
index++;
}
return -1;
}
reverse() {
// prev: menyimpan referensi ke simpul sebelumnya
let prev = null;
let current = this.head;
// melakukan perulangan sampai akhir list
while (current) {
// menyimpan referensi node selanjutnya dalam variabel next
const next = current.next;
// pointer pada node saat ini akan menunjuk ke node sebelumnya
current.next = prev;
// memindahkan prev dan current ke node selanjutnya untuk melanjutkan proses pembalikan
prev = current;
current = next;
}
// setelah berhasil melakukan pembalikan, kemudian membalik rujukan dari head dan tail
this.tail = this.head;
this.head = prev;
}
print() {
let current = this.head;
const result = [];
while (current) {
result.push(current.element);
current = current.next;
}
return result;
}
}