Which XXX would replace the missing statements in the following code.

Which XXX would replace the missing statements in the following code to prepend a node in a doubly-linked list? prepend (list, newNode) if (list.head == null) { list.head = newNode list.tail = newNode } else { XXX list.head = newNode } } newNode. next = list.tail
a. list.head.next = newNode newNode.head = list.next
b. list.head.prev newNode list.head newNode. next.
C. list.head.prev = newNode newNode. next = list.head.
d. list.head. next = newNode

Answer:

To prepend a node in a doubly-linked list, the missing statement in the code should be “list.head.prev = newNode”.

So, the correct answer is C.

This statement will correctly insert the new node at the beginning of the list by setting its previous node as null and its next node as the current head. This will update the pointers of the previous head node, making it the second node in the list.

Then, the new node’s next node should be set to the previous tail node, which will complete the insertion process and update the tail pointer.

Option A is incorrect because it doesn’t update the previous node’s next pointer, while option B is incomplete and has a syntax error. Option D will append the node at the end of the list, not at the beginning.

Hence, the answer of the question is C.

More Answers:

Leave a Comment