Stack (ADT) Simulator
Visualise push, pop, overflow, and underflow operations
Stack Visualization
The stack grows upward. All insertion and deletion happen at the TOP.
Current Code
Current line is highlighted.
Stack rule:
PUSH adds to TOP
POP removes from TOP
PEEK reads TOP only
Operations
Bottom
Maximum Size
Zoom
100%
Current State
Size
0
TOP
-1
Value
—
Stack is ready. Choose PUSH, POP, or PEEK.
Limited mode shows empty placeholder slots. Unlimited mode expands as values are pushed. Use the stack size slider if the stack becomes too tall.
Stack Notes
A stack is a linear data structure where insertion and deletion happen only from one end, called the TOP.
Core Idea
- A stack stores items in a linear order.
- It follows LIFO: Last In, First Out.
- The last value inserted is the first value removed.
- Only the top item can be accessed directly.
- The top of the stack is the active end.
- A stack of plates is a common real-life example.
TOP Pointer
- TOP stores the index of the current top item.
- When the stack is empty, TOP is usually -1.
- After a successful PUSH, TOP increases by 1.
- After a successful POP, TOP decreases by 1.
- PEEK reads stack[TOP] without changing TOP.
- In this visualizer, TOP is shown beside the top item.
Stack as an Array
- The first inserted value is stored at index 0.
- The next value is stored at index 1, and so on.
- TOP points to the highest filled index.
- Empty slots represent unused array positions.
- In limited mode, the array has a fixed maximum size.
- In unlimited mode, the visual stack expands as values are added.
Stack Operations
The three main stack operations are PUSH, POP and PEEK. Each operation works with the TOP of the stack.
PUSH
- Check whether the stack is full.
- If full, output stack overflow.
- If not full, increase TOP by 1.
- Store the new value at stack[TOP].
- The inserted value becomes the new top item.
POP
- Check whether the stack is empty.
- If empty, output stack underflow.
- If not empty, read stack[TOP].
- Decrease TOP by 1.
- The previous top item is removed from the stack.
PEEK
- Check whether the stack is empty.
- If empty, there is no value to return.
- If not empty, read stack[TOP].
- Return the top value.
- Do not remove anything from the stack.
Errors and Boundary Cases
Stack algorithms must check for invalid operations before changing TOP.
Overflow
- Overflow happens when PUSH is attempted on a full stack.
- It only applies when the stack has a fixed maximum size.
- The new value should not be inserted.
- TOP should not change.
Underflow
- Underflow happens when POP is attempted on an empty stack.
- There is no top item to remove.
- TOP remains -1.
- The stack stays empty.
Empty Stack
- An empty stack has no stored items.
- TOP is normally represented as -1.
- POP is not possible.
- PEEK is not possible.
Full Stack
- A limited stack is full when size equals maximum size.
- For an array of size n, the last valid index is n - 1.
- When full, TOP is at the last valid index.
- Another PUSH would cause overflow.
Exam and Programming Notes
These points help when writing pseudocode, explaining algorithms, or answering exam-style questions.
Time Complexity
- PUSH is O(1).
- POP is O(1).
- PEEK is O(1).
- No shifting is needed.
- Only TOP changes during PUSH and POP.
Common Uses
- Undo and redo systems.
- Browser back button history.
- Function call stack.
- Expression evaluation.
- Bracket matching.
- Depth-first search.
Pseudocode Tips
- Always check overflow before PUSH in a fixed stack.
- Always check underflow before POP.
- Use TOP to access the current top item.
- Update TOP after checking the stack condition.
- Do not change TOP during PEEK.
About This Simulation
This stack simulator shows PUSH, POP and PEEK on a vertical stack with the TOP pointer, indexes and highlighted pseudocode, including what happens on overflow and underflow.
Features
- Push, pop and peek operations with animation
- TOP pointer and array indexes shown
- Overflow and underflow detection
- Pseudocode highlighted line by line
- Operation timeline with playback
Explanation
A stack is a linear data structure that follows LIFO: the last element added is the first one removed. Elements are only added and removed at one end, called the top.
How it works
- Push: if the stack is full, report overflow. Otherwise increase top by 1 and store the new value at stack[top].
- Pop: if the stack is empty (top = −1), report underflow. Otherwise return stack[top] and decrease top by 1.
- Peek: return stack[top] without removing it.
- isEmpty checks whether top = −1, and isFull checks whether top = capacity − 1.
Why it matters
Stacks power function calls (the call stack), undo/redo, browser back buttons, expression evaluation and conversion (infix to postfix), bracket matching and depth first search. It is one of the first data structures taught in CSE220.
Frequently Asked Questions
What is the time complexity of stack operations?
Push, pop, peek and isEmpty all take O(1) time, because they only touch the top of the stack.
What is stack overflow and underflow?
Overflow happens when you push onto a stack that is already full. Underflow happens when you pop or peek from an empty stack.
What is the difference between a stack and a queue?
A stack is LIFO: the last element in is the first out, and both operations happen at the top. A queue is FIFO: the first element in is the first out, with insertion at the rear and removal at the front.
Can a stack be implemented with a linked list?
Yes. Push inserts a node at the head and pop removes the head, both in O(1). A linked-list stack has no fixed capacity, so it never overflows unless memory runs out.
Topics covered
- Stack ADT
- LIFO (Last In, First Out)
- Push, Pop and Peek
- Stack Overflow and Underflow
- Array Implementation of a Stack
Tags
- stack
- push
- pop
- peek
- lifo
- overflow
- underflow
Related Simulations
Data Structures
B+ Tree
Insert, delete, search in a B+ tree visualiser
Data Structures
Queue (ADT) Simulator
Show enqueue, dequeue, front, rear, overflow, and underflow.
Data Structures
Circular Queue (ADT) Simulator
Demonstrate wrap-around movement of front and rear pointers.
Data Structures
Linked List Simulator
Insert, delete and search in a linked list stored in arrays, with the start and free pointers and every pointer change shown step by step.

