algoritmauzmanı
Sign in
TREN
4.1Stack with an Array
Lesson 4.1.1

Creating an array stack

A is a structure where elements go in and come out at the top only. The simplest way to build one is a fixed-size 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 and lessons that follow rely on these checks.

What you will learn
  • Which two fields make up an array stack
  • Why the variable starts at -1 and not at 0
  • How you tell an empty or a full stack just by looking at top
5 minTime O(1)Memory O(1)C · Java · Python

1Idea

The structure has two fields: the items array that holds the elements, and . top is the 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 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.

012343081526394-1top2top4toptop
  1. The stack is created. Space is set aside for the array and top is set to -1. Whatever is in the cells does not matter; they are not part of the stack.
  2. isEmpty: top == -1 is true, so the stack is empty. The result is true.
  3. isFull: top is not 4, so the stack is not full. The result is false.
  4. After three calls: 3, 8 and 5 were written and top is 2. isEmpty now returns false.
  5. All five cells are used and top is 4, which is CAPACITY - 1. isFull returns true; one more push would be an .

#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) - 1

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

Creating is one assignment and each check is one comparison. None of them depends on the number of elements.

Memory
O(1)

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 element

Then isEmpty returns false, and a 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?

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?

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 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.