Professional Documents
Culture Documents
Introduction
• Non primitive linear
data structure. Data Element n TOP
• Allows operations at …
one end only. …
• The top element can …
only be accessed at Data Element 3
any time.
Data Element 2
– LIFO (Last-in-first-
Data Element 1
out) data structure.
Thapar University UCS406 - Data Structures and Algorithms 2
Operations
• Two primary operations:
– push() − Pushing (storing) an element on the stack.
– pop() − Removing (accessing) an element from the
stack.
• Other operations for effective functionality:
– peek() − Get the top data element of the stack,
without removing it.
– isFull() − Check if stack is full. OVERFLOW
– isEmpty() − Check if stack is empty. UNDERFLOW
Thapar University UCS406 - Data Structures and Algorithms 3
Stack – Push
push(9) push(1) push(3) push(6) push(4) push(2)
9 1 3 6 4 2
top = 4
4 4 4 4 4 top 4 4 4 4
3 3 3 3 top 6 3 6 3 6 3
2 2 2 top 3 2 3 2 3 2 3 2
top 1 1 1 1 1
1 1 1 1 1 1 1
top 9 9 9 9 9 9
0 0 0 0 0 0 0
top = -1 OVERFLOW
4 6 3 1 9 Nothing
top = 4
4 4 4 4 4 4 4 4
6 3 top 6 3 3 3 3 3 3
3 2 3 2 top 3 2 2 2 2 2
1 1 1 1 1 1 top 1 1 1 1 1
9 0 9 0 9 0 9 0 top 9 0 0 0
top = -1 top = -1
UNDERFLOW