ProgrammingHard

A doubly linked list node has prev and next. What must happen before freeing the node p in the middle of the list?

  1. A. Only p->next must be set to NULL
  2. B. The node must be copied to the stack first
  3. C. Both neighbours must be linked to each other, bypassing p, before free(p) is called
  4. D. Nothing, because free unlinks the node automatically

Correct answer: C. Both neighbours must be linked to each other, bypassing p, before free(p) is called

free does not touch the list, so the neighbours must skip over p first. Otherwise they keep pointers to freed memory.

More C Programming questions

Exams that ask this topic