Data Structures: Understanding Stacks, Queues, and Real-World ICT Applications

Data Structures: Understanding Stacks, Queues, and Real-World ICT Applications
1. Introduction
In our previous guides, we explored foundational algorithms like sorting (Bubble Sort, Insertion Sort) and searching (Linear vs. Binary Search). But to process, sort, and search data effectively, computers must first decide how that data is physically and logically arranged in system memory.
A Data Structure is a specialized format for organizing, processing, retrieving, and storing data in a computer so that it can be used efficiently.
While simple variables hold a single number or text string, and arrays store fixed sequential lists, real-world computing requires dynamic structures that follow strict access rules. Two of the most essential linear data structures every computer science student must master are the Stack and the Queue.
2. Visual Sketchnote: Stack vs. Queue
 ╔══════════════════════════════════════════════════════════════════════════════╗
 ║ STACK VS. QUEUE SKETCHNOTE / QUICK MAP ║
 ╚══════════════════════════════════════════════════════════════════════════════╝

  [ 1. THE STACK (LIFO) ] [ 2. THE QUEUE (FIFO) ]
   "Last In, First Out" "First In, First Out"
   Analogy: Stack of plates / trays Analogy: Morning school assembly line

        ┌─────────────┐ Front (Exit) Rear (Entry)
   PUSH │ Item C │ ▼ (Top) ◄── [ A ] [ B ] [ C ] ◄── ENQUEUE
        ├─────────────┤ DEQUEUE
        │ Item B │
        ├─────────────┤
        │ Item A │ • First item in is the first served
        └─────────────┘ • Deletions at FRONT, insertions at REAR
    POP ▲ (Only from the top!)

 ────────────────────────────────────────────────────────────────────────────────
  [ REAL-WORLD SYSTEM USE CASES ]

   STACK IN ACTION: QUEUE IN ACTION:
   • "Ctrl + Z" (Undo feature in Writer) • School Lab Network Printer Spooler
   • Browser "Back" button history • CPU Task Scheduling (Round Robin)
   • Function call stack in Python/C++ • Student fee counter queues
 ════════════════════════════════════════════════════════════════════════════════

3. The Stack: Last-In, First-Out (LIFO)
A Stack is a linear data structure in which insertions and deletions are restricted to a single end, known as the Top of the Stack.
Operating Principle: LIFO
 * LIFO (Last-In, First-Out): The element added most recently is always the first one to be removed.
 * Think of a vertical stack of cafeteria plates: you place new plates on the top, and whoever takes a plate takes the top one first.
Fundamental Stack Operations:
 * Push: Adds a new item to the top of the stack.
 * Pop: Removes and returns the top item.
 * Peek (or Top): Inspects the value of the top element without removing it.
 * Overflow: Occurs when attempting to push onto a completely full fixed-size stack.
 * Underflow: Occurs when attempting to pop from an empty stack.
Real-World Applications:
 * Undo/Redo Mechanisms: In office software like LibreOffice Writer or Microsoft Word, every typing stroke or format change is pushed onto a stack. When you hit Ctrl + Z, the software pops the most recent action off the top.
 * Web Browser Navigation: Your browser pushes visited URLs onto a history stack; clicking the Back arrow pops the current page to return to the previous one.
4. The Queue: First-In, First-Out (FIFO)
A Queue is an open linear collection where items are inserted at one end (the Rear) and removed from the opposite end (the Front).
Operating Principle: FIFO
 * FIFO (First-In, First-Out): The first element added is always the first one to be processed and removed.
 * Think of students standing in line for morning assembly or a bus ticket counter: whoever arrives first gets served first.
Fundamental Queue Operations:
 * Enqueue: Appends an element to the Rear (tail) of the queue.
 * Dequeue: Removes and retrieves an element from the Front (head) of the queue.
 * Front / Peek: Returns the element currently at the front without deleting it.
Real-World Applications:
 * Printer Spooling (ICT Lab): When multiple workstations in a school lab send examination papers to a shared network laser printer simultaneously, the operating system places jobs into a print queue. Print job #1 is completed before job #2 begins.
 * Keyboard Buffer: Rapid typing stores characters in a hardware FIFO queue so letters render in exact chronological order on screen without skipping.
5. Direct Comparison: Stack vs. Queue
| Characteristic | Stack | Queue |
|---|---|---|
| Guiding Principle | LIFO (Last In, First Out) | FIFO (First In, First Out) |
| Operational Ends | Single end (Top) for both push and pop | Two ends: insert at Rear, remove from Front |
| Primary Actions | Push and Pop | Enqueue and Dequeue |
| Real-World Analogy | Stack of books, tennis balls in a tube | Ticket counter queue, assembly line |
| System Function | Backtracking, function call stacks, undo/redo | Hardware buffering, printer spooling, OS scheduling |
6. Hands-on Lab Activity: Implementing Stacks and Queues in Python
Students can model both data structures directly using Python's built-in list and collections.deque on their lab workstations:
# ===================================================
# ICT Lab Exercise: Demonstrating Stack and Queue
# ===================================================

# --- PART 1: STACK DEMO (Using Python List) ---
print("=== STACK SIMULATION (LIFO) ===")
browser_history = []

# User visits 3 pages (PUSH)
browser_history.append("ictlabpawai.blogspot.com")
browser_history.append("google.com")
browser_history.append("wikipedia.org")
print("History Stack after visits:", browser_history)

# User clicks the 'Back' button (POP)
last_visited = browser_history.pop()
print(f"Clicked Back: Left [{last_visited}]")
print(f"Current active page: [{browser_history[-1]}]")
print("Remaining Stack:", browser_history)

print("\n" + "="*45 + "\n")

# --- PART 2: QUEUE DEMO (Using collections.deque) ---
from collections import deque

print("=== LAB PRINTER QUEUE SIMULATION (FIFO) ===")
printer_spool = deque()

# Workstations send print tasks (ENQUEUE)
printer_spool.append("Class_9_Exam_Papers.pdf")
printer_spool.append("Class_10_Syllabus.docx")
printer_spool.append("Attendance_Register.xlsx")
print("Jobs waiting in spooler:", list(printer_spool))

# Printer processes the first job (DEQUEUE)
served_job = printer_spool.popleft()
print(f"Printing now: [{served_job}]")
print("Remaining jobs in queue:", list(printer_spool))

Lab Verification Steps:
 * Open Python IDLE or the Linux terminal, save the script as stack_queue_lab.py, and run it (F5).
 * Notice that in the Stack, wikipedia.org was the last added and the first popped.
 * In the Queue, Class_9_Exam_Papers.pdf was the first added and the first printed.
7. Review Questions & Key Answers
Q1: What is the main difference between LIFO and FIFO?
 * Answer: LIFO (Last-In, First-Out) removes the newest element first (used by Stacks), while FIFO (First-In, First-Out) removes the oldest element first in chronological sequence (used by Queues).
Q2: What error condition occurs when you attempt to remove an element from an empty Stack?
 * Answer: Stack Underflow.
Q3: Why does a shared network printer use a Queue rather than a Stack to process print jobs?
 * Answer: A printer must process print jobs fairly in the exact order they were received (FIFO). If it used a Stack, earlier print requests would be delayed indefinitely as new jobs were submitted.

Comments

Popular posts from this blog

विद्यार्थियों के लिए संस्कृत का अत्यंत प्रसिद्ध और महत्वपूर्ण श्लोक है।

MS Excel