DSAverse
Basics & Data Structures
Loading Data Structures...
Array Access
12
05
18
23
317
49
5array[0..5]indexed access
Array
Stack
Queue
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Basics & Data Structures
Loading Data Structures...
Array Access
Array
Stack
Queue
Explore LIFO (Last In, First Out) through push, pop, and peek. Watch how the top pointer moves as elements enter and leave.
Elements are added/removed from the TOP only — LIFO principle.
Color Legend
Use Push, Pop, or Peek to begin visualization.
Add element to the top — increment top pointer, assign value
Remove and return top element — decrement top pointer
Read top element without removing it
What does LIFO stand for?
class StackArray:
def __init__(self, max_size):
self.array = [None] * max_size # Fixed-size array
self.top = -1 # Index of top element
self.max_size = max_size
def push(self, value):
if self.top >= self.max_size - 1:
raise Exception("Stack Overflow")
self.top += 1
self.array[self.top] = value
return value
def pop(self):
if self.top < 0:
raise Exception("Stack Underflow")
value = self.array[self.top]
self.array[self.top] = None
self.top -= 1
return value
def peek(self):
if self.top < 0:
raise Exception("Stack is Empty")
return self.array[self.top]
def is_empty(self):
return self.top < 0
def is_full(self):
return self.top >= self.max_size - 1
def size(self):
return self.top + 1