Consider a stack S implemented using a linked list. When does the following…
2022
Consider a stack S implemented using a linked list. When does the following function return TRUE? int f1(stack S) { return S->Next == NULL; } A) When the stack is full. B) When the stack is empty. C) When no memory is available. Mark the correct option.
- A.
Only (A) is correct
- B.
Only (B) is correct
- C.
Only (C) is correct
- D.
Only (B) and (C) are correct
Attempted by 867 students.
Show answer & explanation
Correct answer: B
Answer: The function returns TRUE when the stack is empty (assuming S is a head/sentinel node).
Key idea:
If S is a head (sentinel) node and S->Next points to the first data node, then S->Next == NULL means there are no data nodes — the stack is empty.
If the implementation instead uses a top pointer that is NULL when empty, emptiness is tested with top == NULL rather than checking a Next field.
A linked-list stack normally does not become "full" unless memory is exhausted; there is no fixed capacity to test with S->Next == NULL.
Running out of memory is detected when an allocation call (for example, malloc) returns NULL during a push operation — it is not indicated by S->Next being NULL.
Conclusion: The pointer test S->Next == NULL is used to detect that the stack contains no elements (empty), so the correct choice is the statement that says the stack is empty.