3. What will be the output of the following code snippet after inserting 40 at the end of the doubly linked list?
class Node {
int data;
Node next, prev;
Node(int d) { data = d; next = prev = null; }
}
class DoublyLinkedList {
Node head;
void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node last = head;
while (last.next != null) last = last.next;
last.next = newNode;
newNode.prev = last;
}
void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " <-> ");
temp = temp.next;
}
System.out.println("null");
}
}
public class Main {
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertEnd(10);
dll.insertEnd(20);
dll.insertEnd(30);
dll.insertEnd(40);
dll.printList();
}
}