Stack Ready
size 3 / 6
top 2
status OK
top →
bottom
  • Top / current
  • Pushed / found
  • Popping
  • Empty slot

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

How is a stack stored?

We use an ordinary array plus one number: top, the index of the top element. An empty stack has top = -1. Pushing moves top up, popping moves it down — nothing else ever moves.

#define CAPACITY 6

int stack[CAPACITY];   // the storage
int top = -1;          // -1 means the stack is empty

// top + 1  = number of items
// stack[top] = the item on top

Push

  1. Check the stack is not full (top < CAPACITY − 1).
  2. Move top up by one.
  3. Store the value at stack[top].
  4. Always O(1) — nothing else moves.

Pop

  1. Check the stack is not empty (top ≠ −1).
  2. Read the value at stack[top].
  3. Move top down by one.
  4. Return the value. O(1).

Peek, isEmpty, isFull

  1. Peek returns stack[top] without removing it.
  2. isEmpty is simply: top == −1.
  3. isFull is simply: top == CAPACITY − 1.
  4. All three are O(1).

Balanced brackets

  1. Read the expression left to right.
  2. Push every opening bracket ( [ {.
  3. For a closing bracket, pop and check it matches.
  4. At the end the stack must be empty.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 PUSH(stack, top, value)
2 if top = CAPACITY − 1 then report "Overflow"; stop
3 top ← top + 1
4 stack[top] ← value
Complexity

How fast is each operation?

n = number of items. Push, pop and peek never depend on n.

Operation Best Average Worst Speed
Push O(1) O(1) O(1) Instant
Pop O(1) O(1) O(1) Instant
Peek O(1) O(1) O(1) Instant
isEmpty / isFull O(1) O(1) O(1) Instant
Search for a value O(1) O(n) O(n) Grows with n
Balanced brackets (length n) O(n) O(n) O(n) Grows with n

Stack using an array vs a linked list

Array Linked list
Push / pop O(1) at index top O(1) at the head
Size limit Fixed capacity — can overflow Grows until memory runs out
Extra memory None per item One pointer per item
Simplicity Very simple: array + top Needs node allocation

Where stacks are used

Use What is pushed Why a stack?
Undo in an editor Each action you make The last action is undone first
Browser back button Each page you visit Back goes to the most recent page
Function calls Each call (the call stack) The last function called returns first
Checking brackets Each opening bracket The last opened must close first
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

You push 10, then 20, then 30. What does pop() return?

Question 2

In an array stack, what is the value of top when the stack is empty?

Question 3

What happens if you push onto a full array stack?

Question 4

Is the expression "( [ ) ]" balanced?

Question 5

After clear() on an array stack, where are the old values?

Assessment

Stacks quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.