DSA Series - Chapter 5 : REAL-LIFE EXAMPLES OF STACK

 


REAL-LIFE EXAMPLES OF STACK 

(Simple & Relatable)

1. Plate Dispenser in Restaurants

Plates are stacked vertically.

  • Last plate placed is the first plate taken. → LIFO


2. Books/Notebooks Stack on a Table

Removing the top book before accessing the one below directly shows stack principle.


3. Browser Back/Forward Navigation

  • Every URL visited → pushed onto the back-stack

  • Clicking Back → pop, loading previous URL

  • Forward button uses another stack


4. Undo/Redo Function in Editors (MS Word, Notepad, Photoshop)

  • Every change → push onto undo stack

  • Undo pops last change

  • Redo uses a second stack


5. Pile of Clothes in a Wardrobe

You remove clothes from the top → LIFO.


6. Calls Made from a Phone (Call History)

Recent calls appear first because they are stacked on top.



 ENGINEERING / SYSTEM-LEVEL REAL IMPLEMENTATIONS USING STACKS

These are VERY important for interviews, projects, GATE, and DSA mastery.


1. Function Call Stack (MOST IMPORTANT)

The OS and CPU use a call stack to store:

  • Function return address

  • Local variables

  • Parameters

How it works:

  • When a function is called → its frame is pushed

  • When the function finishes → frame is popped

This is why recursion uses stack.


2. Expression Evaluation (Compiler Design)

Stacks are used in:

  • Infix to Prefix/Postfix conversion

  • Evaluating Postfix expressions

  • Arithmetic expression parsing

Compilers heavily use stack-based parsing.


3. Parentheses Matching / Syntax Validation

Used in:

  • Checking balanced brackets {}, (), []

  • XML/HTML tag validation

  • Compiler syntax checking

Stack tracks opening symbols and checks proper closure.


4. Depth First Search (DFS) Algorithm

DFS uses stack internally.
Even if you write it recursively, recursion uses the system’s call stack.


5. Memory Management in OS (Stack Allocation)

Each program gets stack memory for:

  • Local variables

  • Function calls

  • Temporary variables

  • Return addresses


6. Undo Mechanism in Databases

Database systems (like MySQL, Oracle) use stacked transactions to revert changes.


7. Virtual Machines (Java JVM, Python VM)

Both use an operand stack to store temporary data during instruction execution.


8. Backtracking Algorithms

Stack is used in:

  • Maze solving

  • N-Queens

  • Sudoku solver

  • Pathfinding

  • Robot navigation

It stores previous states and pops when backtracking.


9. Browser JavaScript Execution Context

JS Engine (V8, SpiderMonkey) maintains a call stack:

  • Each JS function creates a new execution context pushed to stack

  • When function returns, context pops out


10. Operating System Interrupt Handling

When an interrupt occurs:

  • Current state is pushed

  • Interrupt service routine runs

  • Previous state is popped back


11. Reversing Items

Using stack to reverse:

  • Strings

  • Arrays

  • Linked lists
    Because stack produces reversed order automatically.


12. Topological Sorting (Kahn’s Algorithm variant)

A stack is used to store nodes with no incoming edges.


13. Syntax Trees and Expression Trees

Used by compilers to store intermediate parsing results.


14. Browser History with Tabs

Each tab maintains its own stack of page states.


 SUMMARY TABLE (QUICK REVISION)

Real-World Example Why Stack? (LIFO)
Plates, books, clothes Last placed is first removed
Browser back button Last visited page returns first
Undo/Redo Last action undone first
Phone call history Most recent displayed first

System/Algorithm Example Where Stack is Used
Function calls, recursion Function call stack
Compilers Parsing, postfix evaluation
DFS Stack-based traversal
Matching brackets Tracking opening brackets
JVM/Python VM Operand stack
Backtracking Saving previous states
Interrupt handling Saving/restoring context

BACK TO STACKS CHAPTER

Comments