-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyList.js
More file actions
96 lines (84 loc) · 2.5 KB
/
DependencyList.js
File metadata and controls
96 lines (84 loc) · 2.5 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
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
// ==========================================
// Node for our Linked List
// ==========================================
class Node {
constructor(taskId) {
this.taskId = taskId;
this.next = null; // Pointer to the next dependency node
}
}
// ==========================================
// DependencyList - Singly Linked List Implementation
// ==========================================
// Manages a sequential chain of task IDs that must be completed.
// We use a tail pointer to ensure O(1) insertions.
class DependencyList {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
// O(1) addition to the tail
append(taskId) {
const newNode = new Node(taskId);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.size++;
}
// O(N) removal by task ID
remove(taskId) {
if (!this.head) return false;
// If the target is the head node
if (this.head.taskId === taskId) {
this.head = this.head.next;
this.size--;
if (this.size === 0) {
this.tail = null;
}
return true;
}
// Traverse to find the gap
let current = this.head;
while (current.next) {
if (current.next.taskId === taskId) {
// Link past the target node
current.next = current.next.next;
this.size--;
// If we severed the tail, re-assign tail
if (current.next === null) {
this.tail = current;
}
return true;
}
current = current.next;
}
return false; // Not found
}
// Empties the list structure
clear() {
this.head = null;
this.tail = null;
this.size = 0;
}
// O(N) traversal to convert back to an array
toArray() {
const result = [];
let current = this.head;
while (current) {
result.push(current.taskId);
current = current.next;
}
return result;
}
// Built-in JavaScript hook triggered during JSON.stringify().
// By returning an Array, our nested Linked List nodes won't clutter the HTTP response!
toJSON() {
return this.toArray();
}
}
module.exports = DependencyList;