The statements of pseudocode for searching the first element with key k in the linked list L are given below. Arrange them in the correct order.
(A) while (x != NIL and x.key != k)
(B) x = L.head
(C) x = x.next
(D) return x
Step 1: Understanding the pseudocode.
To search for an element in a linked list, the steps typically follow this flow:
- **(B)** Set `x = L.head`, as the search starts from the head of the linked list.
- **(A)** Use a **while** loop to check that the current node (`x`) is not `NIL` and the key (`x.key`) is not equal to `k`. This continues until the element is found or the end of the list is reached.
- **(C)** Move to the next node by updating `x = x.next`.
- **(D)** If the node with the desired key is found, return `x`.
Step 2: Conclusion.
The correct order of the steps is **(B), (A), (C), (D)**. This order ensures that the search starts at the head, continues through the list, and terminates when the element is found.