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.

Answer: B. Only (B) is correctConcept: In a linked-list stack, the stack handle keeps a single pointer to the current top element — here that pointer is the handle's Next field. The stack…

  1. A.

    Only (A) is correct

  2. B.

    Only (B) is correct

  3. C.

    Only (C) is correct

  4. D.

    Only (B) and (C) are correct

Attempted by 1099 students.

Show answer & explanation

Correct answer: B

Concept: In a linked-list stack, the stack handle keeps a single pointer to the current top element — here that pointer is the handle's Next field. The stack is empty exactly when this top pointer is NULL, meaning no element is linked. A heap-backed linked stack has no fixed capacity, so there is no "full" state, and running out of memory is an allocator condition rather than a state of the existing links.

Application — trace f1:

  1. S is the whole stack handle (its type is stack), and S->Next is its top-of-stack pointer, holding the address of the most recently pushed node.

  2. S->Next == NULL is true exactly when no node is linked to the handle.

  3. No linked node means zero elements, so f1 returns TRUE precisely when the stack holds no elements — that is, when it is empty.

Contrast the other conditions:

  • Full — nodes are allocated on demand with no preset size limit, so a linked-list stack has no full threshold for a pointer comparison to detect.

  • Memory unavailable — this surfaces when an allocation such as malloc returns NULL during a push; it is a heap event, not the value of S->Next.

  • A common misreading — if S were taken to be the top data node, then S->Next == NULL would mark exactly one element and emptiness would be S == NULL; but the parameter is the stack handle, whose Next field is the top pointer, so S->Next == NULL marks the empty stack.

Cross-check: on an empty stack S->Next is NULL and f1 returns TRUE; after one push, S->Next points to that node and f1 returns FALSE; popping it restores S->Next to NULL and f1 returns TRUE again — consistent with detecting emptiness.

Therefore f1 detects that the stack contains no elements, so the correct statement is the one asserting the stack is empty.

Explore the full course: Bpsc

Loading lesson…