Complete the code to create a node with a value and next pointer.
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = [1]; } }
The next pointer should start as null because the node does not point to any other node yet.
Complete the enqueue method to add a new node at the end of the queue.
enqueue(value: number): void {
const newNode = new Node(value);
if (this.tail === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = [1];
this.tail = newNode;
}
}We set the current tail's next pointer to the new node to link it at the end.
Fix the error in the dequeue method to correctly remove the front node.
dequeue(): number | null {
if (this.head === null) return null;
const value = this.head.value;
this.head = [1];
if (this.head === null) {
this.tail = null;
}
return value;
}To remove the front node, we move the head pointer to the next node.
Fill both blanks to check if the queue is empty and return the correct boolean.
isEmpty(): boolean {
return this.head [1] [2];
}The queue is empty if the head is exactly equal to null.
Fill all three blanks to implement a method that prints the queue values in order.
printQueue(): void {
let current = this.head;
while (current [1] null) {
console.log(current.[2]);
current = current.[3];
}
}We loop while current is not null, print current's value, then move to next node.
