3. What will be the output of the following TypeScript code that creates a doubly linked list with nodes 10, 20, 30 and prints the list forward?
class Node {
data: number;
next: Node | null;
prev: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
head: Node | null = null;
append(data: number) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
printForward() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new DoublyLinkedList();
list.append(10);
list.append(20);
list.append(30);
list.printForward();