-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathDoublyLinkedList.ts
371 lines (308 loc) · 11.1 KB
/
DoublyLinkedList.ts
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
import Node from './DoublyNode.ts';
export default class DoublyLinkedList<T> {
private head: Node<T> | null;
private tail: Node<T> | null;
private length: number;
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
public getLength(): number {
return this.length;
}
public isEmpty(): boolean {
return this.length === 0;
}
public append(value: T, demo?: boolean): boolean {
const newNode = new Node(value);
if (!this.tail) {
this.head = newNode;
this.tail = newNode;
}
else {
newNode.setPrevious(this.tail);
this.tail.setNext(newNode);
this.tail = this.tail.getNext();
}
++this.length;
if (demo) {
console.log('--------- Appending', value, 'at index', this.length-1);
console.log(this.toString());
}
return true;
}
public prepend(value: T, demo?: boolean): boolean {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.setNext(this.head);
this.head.setPrevious(newNode);
this.head = newNode;
}
++this.length;
if (demo) {
console.log('--------- Prepending', value, '---------');
console.log(this.toString());
}
return true;
}
public insert(value: T, atIndex: number, demo?: boolean): boolean | null {
if (atIndex < 0 || atIndex > this.length) return null;
if (atIndex === 0) {
this.prepend(value, demo);
return true;
}
if (atIndex === this.length) {
this.append(value, demo);
return true;
}
const newNode = new Node(value);
const leader = this._traverseToNode(atIndex-1);
if (!leader) return false;
const follower = leader.getNext();
newNode.setPrevious(leader);
newNode.setNext(follower);
leader.setNext(newNode);
follower.setPrevious(newNode);
++this.length;
if (demo) {
console.log('--------- Inserting', value, 'at index', atIndex);
console.log(this.toString());
}
return true;
}
public getValueAtIndex(index: number): T | null {
if (index > this.length-1 || index < 0) return null; // Validate input
if (this.length === 0 || !this.head || !this.tail) return null; // Verify that list is not empty
if (index === this.length-1) return this.tail.getValue(); // Optimization when retrieving last element
let targetNode = this._traverseToNode(index);
return targetNode?.getValue() || null;
}
/**
* Find the index of the desired element if it exists.
* WARNING: Usable only for trees of primitive types, i.e. number, string
* @param value Value to search for, i.e. desired element
* @returns Numerical index of element position
*/
public searchFor(value: T): number | null {
if (this.length === 0 || !this.head) return null;
let currentNode = this.head;
for (let i=0; !!currentNode; ++i) {
if (currentNode.getValue()===value) return i; // Value matches, so return index
currentNode = currentNode.getNext(); // Otherwise, continue searching
}
return null;
}
/**
* Remove all nodes from list. Constant Time O(1)
*/
public empty(nestedCall?: boolean): boolean {
this.head = null;
this.tail = null;
this.length = nestedCall ? 1 : 0;
return true;
}
public removeElementAtIndex(index: number, demo?: boolean): T | null {
if (index > this.length-1 || index < 0) return null;
if (this.length === 0 || !this.head) return null;
let value = null;
if (index===0) {
value = this.head.getValue();
if (this.length > 1) {
const newHead = this.head.getNext();
newHead.setPrevious(null);
this.head.setNext(null);
this.head = newHead;
} else {
this.empty(true);
}
}
else {
let leader = this._traverseToNode(index-1);
const deletedNode = leader?.getNext();
value = deletedNode.getValue();
leader?.setNext(leader.getNext().getNext());
leader?.getNext().setPrevious(leader);
deletedNode.setNext(null);
deletedNode.setPrevious(null);
}
--this.length;
if (demo) {
console.log('Removing element at index', index + ':', JSON.stringify(value));
}
return value;
}
private _traverseToNode(index: number): Node<T> | null {
if (!this.head || !this.tail) return null;
let currentNode: Node<T>;
if (index < this.length/2) {
currentNode = this.head;
for (let i=0; i<index; ++i) {
currentNode = currentNode.getNext();
}
} else {
currentNode = this.tail;
for (let i=this.length-1; i>index; --i) {
currentNode = currentNode.getPrevious();
}
}
return currentNode;
}
public toString(nodesPerGroup?: number): string {
if (this.length === 0 || !this.head) {
return "";
}
nodesPerGroup = nodesPerGroup ? nodesPerGroup : 6;
const LABEL_HEAD = 'HEAD';
const LABEL_TAIL = 'TAIL';
let listAsString: string = `--- Node Count: ${this.length}\n`;
let currentNode: Node<any> = this.head;
for (let i=0; i < this.length; ++i) {
listAsString += `${Array(i%nodesPerGroup).fill('\t').join('')} ${i===0 ? LABEL_HEAD : i} ${currentNode} ${i===this.length-1 ? LABEL_TAIL : ''}\n`;
if (currentNode.hasNext()) {
currentNode = currentNode.getNext();
}
}
return listAsString;
}
}
function printLinkedList(linkedList: DoublyLinkedList<any>): void {
if (linkedList.isEmpty()) {
console.log('Empty linked list -_-');
return;
}
console.log(linkedList.toString());
}
function printSearchFor(value: any, linkedList: DoublyLinkedList<any>): void {
console.log(JSON.stringify(value), 'found at index:', linkedList.searchFor(value));
}
function printGetValueAtIndex(index: number, linkedList: DoublyLinkedList<any>) {
console.log('Element at index', index +':', linkedList.getValueAtIndex(index));
}
//---------------------------------------------------------------------
// ---------- MAIN PROGRAM ----------
//---------------------------------------------------------------------
if (import.meta.main) {
const ATLA = new DoublyLinkedList<string>();
ATLA.append('Sokka');
ATLA.append('Katara');
ATLA.prepend('Appa');
printLinkedList(ATLA);
ATLA.insert('Aang', 2, true);
ATLA.insert('Zuko', 1);
ATLA.insert('Iroh', 5, true);
ATLA.insert('Azula', 0, true);
printSearchFor('Iroh', ATLA);
printSearchFor('Sok', ATLA);
printSearchFor('Sokka', ATLA);
printSearchFor('Appa', ATLA);
printSearchFor('Zuko', ATLA);
printGetValueAtIndex(1, ATLA);
printGetValueAtIndex(6, ATLA);
console.log('----------------------------------');
ATLA.removeElementAtIndex(1, true);
printLinkedList(ATLA);
ATLA.removeElementAtIndex(0, true);
printLinkedList(ATLA);
ATLA.removeElementAtIndex(2, true);
printLinkedList(ATLA);
ATLA.removeElementAtIndex(2, true);
printLinkedList(ATLA);
ATLA.removeElementAtIndex(1, true);
ATLA.removeElementAtIndex(0, true);
ATLA.removeElementAtIndex(1, true);
ATLA.removeElementAtIndex(0, true);
printLinkedList(ATLA);
console.log('----------------------------------');
console.log();
ATLA.insert('Katara', 0, true);
ATLA.append('Aang');
printLinkedList(ATLA);
// RUN: deno run Data-Structures/Linked-Lists/DoublyLinkedList.ts
}
// --------------------------- Terminal Output: ---------------------------
// --- Node Count: 3
// HEAD { value: "Appa", next: true, previous: false }
// 1 { value: "Sokka", next: true, previous: true }
// 2 { value: "Katara", next: false, previous: true } TAIL
//
// --------- Inserting Aang at index 2
// --- Node Count: 4
// HEAD { value: "Appa", next: true, previous: false }
// 1 { value: "Sokka", next: true, previous: true }
// 2 { value: "Aang", next: true, previous: true }
// 3 { value: "Katara", next: false, previous: true } TAIL
//
// --------- Appending Iroh at index 5
// --- Node Count: 6
// HEAD { value: "Appa", next: true, previous: false }
// 1 { value: "Zuko", next: true, previous: true }
// 2 { value: "Sokka", next: true, previous: true }
// 3 { value: "Aang", next: true, previous: true }
// 4 { value: "Katara", next: true, previous: true }
// 5 { value: "Iroh", next: false, previous: true } TAIL
//
// --------- Prepending Azula ---------
// --- Node Count: 7
// HEAD { value: "Azula", next: true, previous: false }
// 1 { value: "Appa", next: true, previous: true }
// 2 { value: "Zuko", next: true, previous: true }
// 3 { value: "Sokka", next: true, previous: true }
// 4 { value: "Aang", next: true, previous: true }
// 5 { value: "Katara", next: true, previous: true }
// 6 { value: "Iroh", next: false, previous: true } TAIL
//
// "Iroh" found at index: 6
// "Sok" found at index: null
// "Sokka" found at index: 3
// "Appa" found at index: 1
// "Zuko" found at index: 2
// Element at index 1: Appa
// Element at index 6: Iroh
// ----------------------------------
// Removing element at index 1: "Appa"
// --- Node Count: 6
// HEAD { value: "Azula", next: true, previous: false }
// 1 { value: "Zuko", next: true, previous: true }
// 2 { value: "Sokka", next: true, previous: true }
// 3 { value: "Aang", next: true, previous: true }
// 4 { value: "Katara", next: true, previous: true }
// 5 { value: "Iroh", next: false, previous: true } TAIL
//
// Removing element at index 0: "Azula"
// --- Node Count: 5
// HEAD { value: "Zuko", next: true, previous: false }
// 1 { value: "Sokka", next: true, previous: true }
// 2 { value: "Aang", next: true, previous: true }
// 3 { value: "Katara", next: true, previous: true }
// 4 { value: "Iroh", next: false, previous: true } TAIL
//
// Removing element at index 2: "Aang"
// --- Node Count: 4
// HEAD { value: "Zuko", next: true, previous: false }
// 1 { value: "Sokka", next: true, previous: true }
// 2 { value: "Katara", next: true, previous: true }
// 3 { value: "Iroh", next: false, previous: true } TAIL
//
// Removing element at index 2: "Katara"
// --- Node Count: 3
// HEAD { value: "Zuko", next: true, previous: false }
// 1 { value: "Sokka", next: true, previous: true }
// 2 { value: "Iroh", next: false, previous: true } TAIL
//
// Removing element at index 1: "Sokka"
// Removing element at index 0: "Zuko"
// Removing element at index 0: "Iroh"
// Empty linked list -_-
// ----------------------------------
//
// --------- Prepending Katara ---------
// --- Node Count: 1
// HEAD { value: "Katara", next: false, previous: false } TAIL
//
// --- Node Count: 2
// HEAD { value: "Katara", next: true, previous: false }
// 1 { value: "Aang", next: false, previous: true } TAIL