Please explain why the answer is what is is thoroughly. I am trying to study for
ID: 3827644 • Letter: P
Question
Please explain why the answer is what is is thoroughly. I am trying to study for my test. I will make sure to thumbs up! thank you very much for your time
11. Which of the following loops will correctly delete all nodes from a linked list named list and release the memory that they occupy? a) struct node *p; while (list NULL) p list list list next free (p) b) struct node *p; for (p list, p NULL; p p next) free (p); c) struct node prev, *cur; for (prev NULL, cur list; cur! NULL; cur cur- next) prev Curr free (prev) d) A or C e) None of the above. AnswerExplanation / Answer
A is only correct answer
Basically for freeing memory of a list you need to free memory individually of each node.
While freeing memory of a given node you need to hold reference to its next pointer otherwsie you cannot access next pointer and hence can't free memory for rest of the list.
On a side note, In B and C, next pointer is obtained after freeing the memory and hence they won't work.