ProgrammingModerate
A cyclic list has a head node whose next points back to itself. What does a naive while (p) loop starting from head do?
- A. It visits each node once and stops at head
- B. It stops after one iteration
- C. It crashes at compile time
- D. It never ends, because p never becomes NULL
Correct answer: D. It never ends, because p never becomes NULL
A cycle means p is never NULL, so the loop runs forever. Floyd's method or a visited check is needed to detect it.
More C Programming questions
- In C, what is the value of a freshly malloc'd int that the program never initialises?
- A linked list is reversed with the recursive function rev(n): if (!n || !n->next) return n; h = rev(n->next); n->next->next = n; n->next =…
- A malloc'd array of 10 structs is later passed to free(&arr[3]). What is the correct description?
- Which declaration allocates a 3 x 4 int matrix as one contiguous block with a single free needed?
- A list of N nodes is freed with free() on each node but the next pointer is read after freeing it. Which order is correct?
- C(0) = 1 and C(n) = sum over i from 0 to n-1 of C(i) C(n-1-i). What is C(4)?