Data structures are specialized ways of organizing and storing data to enable efficient access and modification. Different structures are optimized for different operations.
A Stack is like a stack of plates - you add (push) and remove (pop) from the top only. The last item added is the first one removed.
Algorithm StackExample
var stack[10] # Array to hold stack elements
var top = -1 # Points to top element (-1 means empty)
# Push operation
Procedure Push(value)
top = top + 1
stack[top] = value
Print "Pushed:", value
Endprocedure
# Pop operation
Function Pop()
If top == -1 Then
Print "Stack is empty!"
Return -1
Else
var value = stack[top]
top = top - 1
Return value
Endif
Endfunction
# Test the stack
Print "=== STACK OPERATIONS ==="
Print ""
Push(10)
Push(20)
Push(30)
Print ""
Print "Popping elements:"
Print "Popped:", Pop() # Returns 30 (last in)
Print "Popped:", Pop() # Returns 20
Print "Popped:", Pop() # Returns 10 (first in)
Endalgorithm
A Queue is like a line of people - first person in line is first to be served. Add to the back (enqueue), remove from the front (dequeue).
Algorithm QueueExample
var queue[10]
var front = 0
var rear = -1
var size = 0
# Enqueue operation
Procedure Enqueue(value)
rear = rear + 1
queue[rear] = value
size = size + 1
Print "Enqueued:", value
Endprocedure
# Dequeue operation
Function Dequeue()
If size == 0 Then
Print "Queue is empty!"
Return -1
Else
var value = queue[front]
front = front + 1
size = size - 1
Return value
Endif
Endfunction
# Test the queue
Print "=== QUEUE OPERATIONS ==="
Print ""
Enqueue(10)
Enqueue(20)
Enqueue(30)
Print ""
Print "Dequeuing elements:"
Print "Dequeued:", Dequeue() # Returns 10 (first in)
Print "Dequeued:", Dequeue() # Returns 20
Print "Dequeued:", Dequeue() # Returns 30 (last in)
Endalgorithm
Algorithm TaskQueue
var tasks[20]
var taskFront = 0
var taskRear = -1
var taskCount = 0
Procedure AddTask(taskName)
taskRear = taskRear + 1
tasks[taskRear] = taskName
taskCount = taskCount + 1
Print "✅ Task added:", taskName
Endprocedure
Function ProcessTask()
If taskCount == 0 Then
Return "No tasks"
Else
var task = tasks[taskFront]
taskFront = taskFront + 1
taskCount = taskCount - 1
Return task
Endif
Endfunction
Print "=== TASK MANAGER ==="
Print ""
# Add tasks
AddTask("Write report")
AddTask("Send emails")
AddTask("Review code")
Print ""
Print "Processing tasks..."
Print "Working on:", ProcessTask()
Print "Working on:", ProcessTask()
Print "Working on:", ProcessTask()
Endalgorithm
Algorithm UndoSystem
var actions[20]
var actionTop = -1
Procedure DoAction(action)
actionTop = actionTop + 1
actions[actionTop] = action
Print "Action:", action
Endprocedure
Function Undo()
If actionTop == -1 Then
Return "Nothing to undo"
Else
var lastAction = actions[actionTop]
actionTop = actionTop - 1
Return "Undoing: " + lastAction
Endif
Endfunction
Print "=== TEXT EDITOR ==="
Print ""
DoAction("Type 'Hello'")
DoAction("Add space")
DoAction("Type 'World'")
Print ""
Print Undo() # Undo "Type 'World'"
Print Undo() # Undo "Add space"
Endalgorithm
A Linked List is a sequence of nodes where each node contains data and a reference (link) to the next node. Unlike arrays, elements aren't stored contiguously in memory.
Feature | Array | Linked List |
---|---|---|
Access | Fast (direct index) | Slow (must traverse) |
Insert/Delete | Slow (shift elements) | Fast (change links) |
Size | Fixed | Dynamic |
# Conceptual representation (simplified)
# Node structure: [data | next]
# List: 10 → 20 → 30 → null
# ^ ^ ^
# head middle end
Structure | Order | Best For |
---|---|---|
Array | Indexed | Random access, fixed size collections |
Stack | LIFO | Undo operations, backtracking |
Queue | FIFO | Task scheduling, breadth-first traversal |
Linked List | Sequential | Frequent insertions/deletions |