Question:

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 

Show Hint

The search algorithm in a linked list starts from the head, checks for the key, moves to the next node, and returns the node once the element is found.
Updated On: Sep 25, 2025
  • (B), (A), (C), (D)
  • (A), (B), (C), (D)
  • (C), (B), (A), (D)
  • (C), (B), (D), (A)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

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. 
 

Was this answer helpful?
0
0

Questions Asked in CUET PG exam

View More Questions