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 $$ \text{ (A) while ($x \ne$ NIL and $x.key \ne k$) } $$ $$ \text{ (B) $x=L.head$ } $$ $$ \text{ (C) $x = x.next$ } $$ $$ \text{ (D) return $x$ } $$
Step-by-step Solution:
We are asked to arrange the pseudocode steps to search for the first element with key k in a linked list L.
Step-wise reasoning:
x = L.head
→ Start by setting x to the head of the list.while (x ≠ NIL and x.key ≠ k)
→ Traverse the list until either the key is found or the list ends.x = x.next
→ Move to the next node during traversal.return x
→ Once the loop ends, return x.
If found, x is the node with key k; otherwise, NIL.
✅ Correct Order: (B), (A), (C), (D)
✅ Correct Answer: (C)