4.1Stack with an Array
Creating an array stack
A stack is a structure where elements go in and come out at the top only. The simplest way to build one is a fixed-size array plus a single integer that marks the top. This lesson covers the structure itself and two checks: is the stack empty, is it full? The push and pop lessons that follow rely on these checks.
- Which two fields make up an array stack
- Why the top variable starts at -1 and not at 0
- How you tell an empty or a full stack just by looking at top
1Idea
The structure has two fields: the items array that holds the elements, and top. top is the index of the top element. The bottom element is always in items[0]; a new element is written one cell to the right of top.
An empty stack has no top element. That is why top starts at -1: at 0 it would look as if items[0] held an element. The empty check is top == -1 and the full check is top == CAPACITY - 1.
You do not need a separate counter: the number of elements is always top + 1.
In C the capacity is fixed with #define, and the functions take the structure through a pointer so the struct is not copied. In Java and Python the capacity goes to the constructor and the operations are methods of the class.
2Step by step
The drawing shows an array of 5 cells. The number under each cell is its index. The box on the right shows the value of top, and the label above a cell marks the top cell.
- The stack is created. Space is set aside for the array and
topis set to -1. Whatever is in the cells does not matter; they are not part of the stack. isEmpty:top == -1is true, so the stack is empty. The result is true.isFull:topis not 4, so the stack is not full. The result is false.- After three push calls: 3, 8 and 5 were written and
topis 2.isEmptynow returns false. - All five cells are used and
topis 4, which isCAPACITY - 1.isFullreturns true; one more push would be an overflow.
#define CAPACITY 5 struct Stack { int items[CAPACITY]; int top; // index of the top element}; void create(struct Stack* s) { s->top = -1; // empty stack} bool isEmpty(struct Stack* s) { return s->top == -1;} bool isFull(struct Stack* s) { return s->top == CAPACITY - 1;}class ArrayStack { int[] items; int top = -1; // empty stack ArrayStack(int capacity) { items = new int[capacity]; } boolean isEmpty() { return top == -1; } boolean isFull() { return top == items.length - 1; }}class ArrayStack: def __init__(self, capacity): self.items = [None] * capacity self.top = -1 # empty stack def is_empty(self): return self.top == -1 def is_full(self): return self.top == len(self.items) - 1The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Creating is one assignment and each check is one comparison. None of them depends on the number of elements.
The checks use no extra space. The array itself takes room for the full capacity, that is O(k).
The capacity is fixed up front. For a stack that grows when needed, turn the array into a dynamic array or use a linked list.
4Common mistake
The most common mistake is starting top at 0:
s->top = 0; // wrong: looks like the stack holds one elementThen isEmpty returns false, and a pop on the empty stack reads whatever value happens to be in items[0]. Some books define top as the next free cell and start it at 0; with that definition every check changes too. Do not mix the two definitions.
5Try it
An array stack with capacity 5 has top = 3. How many elements does it hold?
The answer is B. top is the index of the top element, and indexes start at 0. Cells 0, 1, 2 and 3 are used: top + 1 = 4 elements.
Show the answer
The answer is B. top is the index of the top element, and indexes start at 0. Cells 0, 1, 2 and 3 are used: top + 1 = 4 elements.
Why does isFull check top == CAPACITY - 1 instead of top == CAPACITY?
The answer is A. In a 5-cell array the indexes run from 0 to 4. When top is 4 every cell is used. A top == 5 check is never true, and the next push would write past the end of the array.
Show the answer
The answer is A. In a 5-cell array the indexes run from 0 to 4. When top is 4 every cell is used. A top == 5 check is never true, and the next push would write past the end of the array.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.