Stacks (ADTs)
This addresses M1SO2,3 of the CS Unit 2 syllabus.
Edu Level: Unit2
Date: Aug 6 2026 - 8:08 PM
⏱️Read Time:
STACKS
STACKS
- An abstract data type with bounded (predefined) capacity
- Follows LIFO (Last In First Out)
- Data can only be accessed from the top of the structure
- A stack is referenced via a pointer to the top element (last added element)
- Implementations: Array (bounded) and Linked List (dynamic)
- Overflow (stack full) and Underflow (stack empty)
- Top pointer tracks last added element
- Link member in last node is set to NULL to indicate bottom (in linked list implementation)
OPERATIONS OF A STACK
push()→ inserts (stores) element at the toppop()→ removes (accesses) element from the toppeek()→ returns top element without removing itisFull()→ checks if stack is fullisEmpty()→ checks if stack is emptycreateStack()→ creates an empty stack
PUSH ALGORITHM
- Check if stack is full → throw Overflow error
- Increment Top pointer
- Insert new element at position pointed to by Top
Begin procedure push: stack, data
If stack is full
Print (“Overflow Error”)
Else
top = top +1
stack[top] = data
Endif
POP ALGORITHM
- Check if stack is empty → throw Underflow error
- Return element at Top
- Decrement Top pointer
Begin procedure pop: (stack, data)
If top == -1
Print (“Underflow”)
Else
top = top – 1
Print (“Element has been popped out”)
Endif
EXAMPLES
push(A)→ A added, Top points to Apush(B)→ B added, Top points to Bpop()→ B removed, Top points to Apush(C)→ C added, Top points to C
If you are still unsure about how stacks work, please watch this video.
Remember all notes are more accurate to the CAPE syllabus than videos