|
| 1 | +//Activity 1: Linked List |
| 2 | + |
| 3 | +// Task 1: Implement a `Node` class |
| 4 | + |
| 5 | +class Node { |
| 6 | + constructor(value = null) { |
| 7 | + this.value = value; |
| 8 | + this.next = null; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +// Task 2: Implement a `LinkedList` class |
| 14 | + |
| 15 | + |
| 16 | +class LinkedList { |
| 17 | + constructor() { |
| 18 | + this.head = null; |
| 19 | + } |
| 20 | + |
| 21 | + add(value) { |
| 22 | + const newNode = new Node(value); |
| 23 | + if (!this.head) { |
| 24 | + this.head = newNode; |
| 25 | + } else { |
| 26 | + let current = this.head; |
| 27 | + while (current.next) { |
| 28 | + current = current.next; |
| 29 | + } |
| 30 | + current.next = newNode; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + remove(value) { |
| 35 | + if (!this.head) return; |
| 36 | + |
| 37 | + if (this.head.value === value) { |
| 38 | + this.head = this.head.next; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + let current = this.head; |
| 43 | + while (current.next && current.next.value !== value) { |
| 44 | + current = current.next; |
| 45 | + } |
| 46 | + |
| 47 | + if (current.next) { |
| 48 | + current.next = current.next.next; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + display() { |
| 53 | + let current = this.head; |
| 54 | + const nodes = []; |
| 55 | + while (current) { |
| 56 | + nodes.push(current.value); |
| 57 | + current = current.next; |
| 58 | + } |
| 59 | + console.log(nodes.join(' -> ')); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +// Activity 2: Stack |
| 65 | + |
| 66 | +// Task 3: Implement a `Stack` class |
| 67 | + |
| 68 | +class Stack { |
| 69 | + constructor() { |
| 70 | + this.items = []; |
| 71 | + } |
| 72 | + |
| 73 | + push(element) { |
| 74 | + this.items.push(element); |
| 75 | + } |
| 76 | + |
| 77 | + pop() { |
| 78 | + if (this.items.length === 0) return "Underflow"; |
| 79 | + return this.items.pop(); |
| 80 | + } |
| 81 | + |
| 82 | + peek() { |
| 83 | + if (this.items.length === 0) return "No elements in Stack"; |
| 84 | + return this.items[this.items.length - 1]; |
| 85 | + } |
| 86 | + |
| 87 | + isEmpty() { |
| 88 | + return this.items.length === 0; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +// Task 4: Use the `Stack` class to reverse a string |
| 94 | + |
| 95 | +function reverseString(str) { |
| 96 | + const stack = new Stack(); |
| 97 | + for (let i = 0; i < str.length; i++) { |
| 98 | + stack.push(str[i]); |
| 99 | + } |
| 100 | + |
| 101 | + let reversedStr = ''; |
| 102 | + while (!stack.isEmpty()) { |
| 103 | + reversedStr += stack.pop(); |
| 104 | + } |
| 105 | + |
| 106 | + return reversedStr; |
| 107 | +} |
| 108 | + |
| 109 | +console.log(reverseString("hello")); // Output: "olleh" |
| 110 | + |
| 111 | + |
| 112 | +// Activity 3: Queue |
| 113 | + |
| 114 | +// Task 5: Implement a `Queue` class |
| 115 | + |
| 116 | + |
| 117 | +class Queue { |
| 118 | + constructor() { |
| 119 | + this.items = []; |
| 120 | + } |
| 121 | + |
| 122 | + enqueue(element) { |
| 123 | + this.items.push(element); |
| 124 | + } |
| 125 | + |
| 126 | + dequeue() { |
| 127 | + if (this.isEmpty()) return "Underflow"; |
| 128 | + return this.items.shift(); |
| 129 | + } |
| 130 | + |
| 131 | + front() { |
| 132 | + if (this.isEmpty()) return "No elements in Queue"; |
| 133 | + return this.items[0]; |
| 134 | + } |
| 135 | + |
| 136 | + isEmpty() { |
| 137 | + return this.items.length === 0; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +//Task 6: Use the `Queue` class to simulate a simple printer queue |
| 142 | + |
| 143 | + |
| 144 | +function printerQueue(jobs) { |
| 145 | + const queue = new Queue(); |
| 146 | + jobs.forEach(job => queue.enqueue(job)); |
| 147 | + |
| 148 | + while (!queue.isEmpty()) { |
| 149 | + console.log(`Printing job: ${queue.dequeue()}`); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +printerQueue(['job1', 'job2', 'job3']); |
| 154 | +// Output: |
| 155 | +// Printing job: job1 |
| 156 | +// Printing job: job2 |
| 157 | +// Printing job: job3 |
| 158 | + |
| 159 | + |
| 160 | +// Activity 4: Binary Tree |
| 161 | + |
| 162 | +//Task 7: Implement a `TreeNode` class |
| 163 | + |
| 164 | + |
| 165 | +class TreeNode { |
| 166 | + constructor(value = null) { |
| 167 | + this.value = value; |
| 168 | + this.left = null; |
| 169 | + this.right = null; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +//Task 8: Implement a `BinaryTree` class |
| 175 | + |
| 176 | +class BinaryTree { |
| 177 | + constructor() { |
| 178 | + this.root = null; |
| 179 | + } |
| 180 | + |
| 181 | + insert(value) { |
| 182 | + const newNode = new TreeNode(value); |
| 183 | + if (!this.root) { |
| 184 | + this.root = newNode; |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + let current = this.root; |
| 189 | + while (true) { |
| 190 | + if (value < current.value) { |
| 191 | + if (!current.left) { |
| 192 | + current.left = newNode; |
| 193 | + return; |
| 194 | + } |
| 195 | + current = current.left; |
| 196 | + } else { |
| 197 | + if (!current.right) { |
| 198 | + current.right = newNode; |
| 199 | + return; |
| 200 | + } |
| 201 | + current = current.right; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + inOrderTraversal(node = this.root, result = []) { |
| 207 | + if (node) { |
| 208 | + this.inOrderTraversal(node.left, result); |
| 209 | + result.push(node.value); |
| 210 | + this.inOrderTraversal(node.right, result); |
| 211 | + } |
| 212 | + return result; |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | +// Activity 5: Graph (Optional) |
| 218 | + |
| 219 | +//Task 9: Implement a `Graph` class |
| 220 | + |
| 221 | + |
| 222 | +class Graph { |
| 223 | + constructor() { |
| 224 | + this.adjList = new Map(); |
| 225 | + } |
| 226 | + |
| 227 | + addVertex(vertex) { |
| 228 | + if (!this.adjList.has(vertex)) { |
| 229 | + this.adjList.set(vertex, []); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + addEdge(vertex1, vertex2) { |
| 234 | + if (this.adjList.has(vertex1)) { |
| 235 | + this.adjList.get(vertex1).push(vertex2); |
| 236 | + } |
| 237 | + if (this.adjList.has(vertex2)) { |
| 238 | + this.adjList.get(vertex2).push(vertex1); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + bfs(startingNode) { |
| 243 | + let visited = new Set(); |
| 244 | + let queue = [startingNode]; |
| 245 | + |
| 246 | + while (queue.length > 0) { |
| 247 | + let currentNode = queue.shift(); |
| 248 | + |
| 249 | + if (!visited.has(currentNode)) { |
| 250 | + console.log(currentNode); |
| 251 | + visited.add(currentNode); |
| 252 | + |
| 253 | + let neighbors = this.adjList.get(currentNode); |
| 254 | + for (let neighbor of neighbors) { |
| 255 | + if (!visited.has(neighbor)) { |
| 256 | + queue.push(neighbor); |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | + |
| 265 | +// Task 10: Use the `Graph` class to represent a simple network and perform BFS |
| 266 | + |
| 267 | + |
| 268 | +const graph = new Graph(); |
| 269 | +graph.addVertex('A'); |
| 270 | +graph.addVertex('B'); |
| 271 | +graph.addVertex('C'); |
| 272 | +graph.addVertex('D'); |
| 273 | +graph.addVertex('E'); |
| 274 | + |
| 275 | +graph.addEdge('A', 'B'); |
| 276 | +graph.addEdge('A', 'C'); |
| 277 | +graph.addEdge('B', 'D'); |
| 278 | +graph.addEdge('C', 'E'); |
| 279 | + |
| 280 | +graph.bfs('A'); |
| 281 | +// Output: |
| 282 | +// A |
| 283 | +// B |
| 284 | +// C |
| 285 | +// D |
| 286 | +// E |
0 commit comments