The initial condition of a stack pointer in an empty stack is?

2023

The initial condition of a stack pointer in an empty stack is?

  1. A.

    0

  2. B.

    -1

  3. C.

    Null

  4. D.

    None of the above

Attempted by 596 students.

Show answer & explanation

Correct answer: B

Concept

A stack pointer (commonly called top) records the location of the most recently pushed element. In the standard array-based stack, top stores the index of that element, and valid array indices begin at 0. So an empty stack must use a sentinel that lies just below the first valid index, signalling that no element exists yet.

Application

  1. Index 0 is the position of the very first element once it is pushed, so 0 cannot mean "empty".

  2. The empty-stack sentinel is therefore the index one below 0, i.e. −1.

  3. The emptiness test is exactly top == −1, and each push first increments top (−1 → 0 → 1 …).

Hence the initial condition of the stack pointer in an empty array-based stack is −1.

Contrast and note on ambiguity

  • 0 — this is the first occupied index after one push, not the empty state.

  • Null — this is how a linked-list stack marks emptiness (the head/top reference points to nothing). It is correct only for the pointer-based variant; for the index-based stack assumed by the standard convention, the empty marker is the numeric −1.

  • None of the above — ruled out, since the array convention gives a definite value.

The question follows the dominant array-implementation convention used in data-structure courses and exam keys, where the stack pointer is an integer index; under that convention the answer is −1.

Explore the full course: Cocubes Preparation