In an array-based stack, top is commonly set to -1 when the stack is created. That value means the stack is empty. The reason is that array positions start at 0, so -1 can never be the position of a real item. It works as a clear marker for “nothing stored yet,” and it keeps the empty check simple.
What top means in an array-based stack
In this setup, top holds the index of the most recently pushed item. If the stack holds three items, top is 2 and the newest item sits at stack[2].
When the stack is empty, no index points to an item. The variable still needs a value, so it needs one that cannot be a real index. That is the job -1 does.
Why -1 works as the empty marker
Array indexes in most common languages, including C, Java, and Python, start at 0. The first valid position is 0, and -1 sits just below it.
Compare that with setting top to 0 when the stack is empty. Then top would be 0 both for an empty stack and for a stack holding one item at index 0. The code would have to track the size some other way to tell the two states apart.
Setting top to -1 avoids that problem. The value is outside the valid range of positions, so it cannot be confused with a stored item.
Push and pop with top starting at -1
The two operations move top in opposite order:
- Push first increases
topby one, then stores the new item at that position. - Pop first reads the item at
stack[top], then decreasestopby one.
Starting from -1, push A moves top to 0 and stores A there. Push B moves top to 1 and stores B there. A pop returns B and sets top back to 0. A second pop returns A and sets top back to -1, which is the empty state again.
This is also why the empty check needs no separate counter. Once top is -1, the stack is empty.
Checking for empty and full
With top at -1 for an empty stack, the checks look like this:
- Empty:
topequals -1. - Full:
topequals the capacity minus one, which is the last valid index.
Using -1 for empty and the last index for full keeps both checks to one comparison each. Popping before the empty check runs would read index -1, which is outside the array. In C, reading outside an array this way is undefined behavior and can crash the program or return unrelated data.
The other convention: top as the next free slot
Some textbooks use a different setup. Here top points to the next free position rather than the last used one. An empty stack has top equal to 0.
- Push stores the item at
stack[top], then increasestopby one. - Pop decreases
topby one, then returnsstack[top]. - Empty is
topequal to 0, and full istopequal to the capacity.
Both conventions work. The important part is to use one set of rules for push, pop, and the checks. Mixing them is a common source of bugs, such as testing for an empty stack with top == 0 in code that uses the -1 convention.
Linked-list stacks
A stack built on a linked list does not use an index, so this question does not come up. The top is a reference to the head node. An empty stack has no head, which is usually stored as null or None. For a comparison of the two storage approaches, see stack operations in data structure.
Common mistakes
- Mixing conventions. Checking for an empty stack with the wrong value gives wrong results on the first push or pop.
- Skipping the empty check. Popping an empty stack reads an invalid position.
- Forgetting the full check. A fixed-size array pushed past its last index overflows.
Summary
- In an array-based stack,
topis commonly -1 when the stack is empty. - -1 is outside the valid index range, so it cannot be mistaken for a stored item.
- Push increases
topthen stores; pop reads then decreases. - The alternative convention uses
topas the next free slot, with an empty value of 0. - Whichever convention you choose, use it consistently in every check.