1 of 25

M4: Stacks and Queues in Real Systems

Controlling Order, Managing Flow

Prof. Justin David Pineda CISSP, CISM

2 of 25

On the Train exercise..

  • Stations List
  • Train Cars
  • Seats Per Car
  • Train Schedule/ Timetable
  • Passenger Demand Matrix (2d)

  • Reservations
  • Waiting list
  • Active trains
  • Event logs
  • Seat history (optional)

3 of 25

User POV

The passenger can:

  • view stations
  • choose origin and destination
  • view available trains
  • reserve a seat
  • receive seat number
  • cancel reservation
  • join waiting list if full

Likely structures involved:

  • station array
  • schedule array
  • car/seat arrays
  • reservation linked list
  • waiting list linked list

4 of 25

Admin POV

The admin can:

  • add/edit train schedules
  • monitor seat occupancy
  • add skip trains during peak hours
  • review passenger demand
  • manage waiting lists
  • cancel or modify reservations
  • check event logs
  • monitor synchronization of regular and skip trains

Likely structures involved:

  • schedule array
  • demand 2D array
  • active train linked list
  • event log linked list
  • reservation linked list

5 of 25

Motivation: Why Order Matters in Systems

  • How does your browser “go back”?
  • How do print jobs line up?
  • Why do systems process requests in order?

Analogy:

Stack = plates in a cafeteria

Queue = line at MRT/LRT

6 of 25

Learning Objectives

After this lesson, students should be able to:

  • Explain Stack (LIFO) and Queue (FIFO)
  • Implement both using arrays and linked lists
  • Analyze time complexity
  • Apply in real-world systems (OS, apps, networks)

7 of 25

What is a Stack?

  • Linear data structure
  • Follows LIFO (Last In, First Out)
  • Only one access point: TOP

Example:

  • Push → Add
  • Pop → Remove

8 of 25

Stack Operations

  • push() → insert element
  • pop() → remove element
  • peek() → view top element
  • isEmpty() / isFull()

9 of 25

Stack in C (Array Implementation)

10 of 25

Stack Visualization

Step-by-step:

Push 10 → Push 20 → Push 30

Pop → removes 30

11 of 25

Stack Applications

  • Undo/Redo (text editors)
  • Function calls (call stack)
  • Expression evaluation (postfix)

12 of 25

Stack Limitations

13 of 25

What is a Queue?

  • Linear data structure
  • Follows FIFO (First In, First Out)

Two ends:

    • Front (remove)
    • Rear (insert)

14 of 25

Queue Operations

  • enqueue() → insert
  • dequeue() → remove
  • peek() → view front

15 of 25

Queue in C (Array Implementation)

16 of 25

Visualization: Insert vs Array Shift

17 of 25

Circular Queue Concept

  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List

18 of 25

Queue Applications

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

19 of 25

Stack vs Queue Comparison

Feature

Stack

Queue

Order

LIFO

FIFO

Access

One end

Two ends

Use Case

Undo

Scheduling

20 of 25

Linked List Implementation Insight

  • Dynamic size
  • No overflow (until memory full)

Stack:

  • Insert/Delete at head

Queue:

  • Insert at tail, delete at head

21 of 25

Real-World System Mapping

  • OS:
    • Stack → function calls
    • Queue → process scheduling
  • Networking:
    • Queue → packet buffering

22 of 25

Key Takeaways

  • Stack = LIFO → last comes out first
  • Queue = FIFO → first comes out first
  • Both critical for system flow control
  • Choice depends on use case

23 of 25

Summary

  • Stacks and Queues manage order of execution
  • Widely used in systems, apps, and networks
  • Implementation affects performance

24 of 25

Stack or Queue?

  • Laundry Pile
  • MRT / LRT Line
  • Desk Papers / Work Tasks
  • Call Center Queue
  • Undo Feature in a Mobile App
  • Plate Serving at a Buffet
  • Browser Back Button
  • Emergency Task Handling
  • Elevator Emergency Stop Buttons
  • Fast Food Order Line
  • Browser Tabs (Recent Focus)
  • Traffic at Intersection
  • Customer Service Hotline
  • Airport Check-in Line
  • Printer Jobs in Office
  • Food Delivery Orders

25 of 25

Knowledge Check (Recitation)

  1. Explain LIFO using a real-life example
  2. What happens during stack overflow?
  3. Difference between enqueue and push?
  4. Why is queue used in CPU scheduling?
  5. When would you choose stack over queue?

6. Give a real-life scenario where a queue would be unfair to use. Why?

7. In your daily work, when do you prioritize the latest task instead of the first task? What structure does this represent?

8. What problem does a circular queue solve compared to a normal queue? (Explain in simple terms)

9. Can a system use both stack and queue at the same time? Give a practical example.

10. If a system processes the most urgent tasks first, is it still a pure queue? Why or why not?