Open In App

Data Structures | Linked List | Question 17

Like Article
Like
Save
Share
Report

Consider the following function to traverse a linked list. 

C




void traverse(struct Node *head)
{
   while (head->next != NULL)
   {
       printf(\"%d  \", head->data);
       head = head->next;
   }
}


Which of the following is FALSE about above function?

(A)

The function may crash when the linked list is empty

(B)

The function doesn\’t print the last node when the linked list is not empty

(C)

The function is implemented incorrectly because it changes head

(D)

None of the above


Answer: (C)

Explanation:

This statement is false because it is common for a function that traverses a linked list to change the head pointer, in order to move through the list. In the given function, the head pointer is initially passed in as a parameter, and then it is updated to point to the next node in the list during each iteration of the while loop. This is a standard approach to traversing a linked list.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads