Practical DSA for Developers Stacks and Queues with Real-World Coding Examples

Practical DSA for Developers: Stacks and Queues in Real-World Applications

Stacks and Queues are two of the most underrated data structures. Many developers think they’re only good for solving interview puzzles, but in reality, they are the backbone of everyday features like undo-redo, browser navigation, and job scheduling.

Let’s explore how they appear in real applications and how you can code them efficiently.

The Stack: Last In, First Out (LIFO)

A stack works just like a pile of plates. The last plate you put on top is the first one you take off. In development, stacks are used for undo features, call stacks in programming languages, and parsing expressions.

Use Case 1: Undo Feature in a Text Editor

JavaScript Example

class Editor {
  constructor() {
    this.stack = [];
  }
  type(text) {
    this.stack.push(text);
  }
  undo() {
    return this.stack.pop();
  }
}

let editor = new Editor();
editor.type("Hello");
editor.type("World");
console.log(editor.undo()); // World

Python Example

stack = []
stack.append("Hello")
stack.append("World")
print(stack.pop())  # World

This is how text editors like VS Code or Google Docs implement undo and redo operations.

Use Case 2: Browser Back Button

Every time you visit a new page, it gets added to the stack. Pressing back simply pops the last entry.

Java Example

import java.util.*;

public class BrowserHistory {
    public static void main(String[] args) {
        Stack<String> history = new Stack<>();
        history.push("google.com");
        history.push("github.com");
        System.out.println(history.pop()); // github.com
    }
}

The Queue: First In, First Out (FIFO)

A queue works like a line at the supermarket. The first one in line gets served first. In applications, queues are used for task scheduling, background jobs, and order processing.

Use Case 1: Task Scheduling in a Server

Python Example using deque

from collections import deque

queue = deque()
queue.append("Task1")
queue.append("Task2")
queue.append("Task3")

while queue:
    print("Processing:", queue.popleft())

Output:

Processing: Task1
Processing: Task2
Processing: Task3

This is how background job systems like Celery, RabbitMQ, or Kafka process tasks.

Use Case 2: Print Queue in an Operating System

JavaScript Example

class PrintQueue {
  constructor() {
    this.queue = [];
  }
  addJob(job) {
    this.queue.push(job);
  }
  processJob() {
    return this.queue.shift();
  }
}

let printer = new PrintQueue();
printer.addJob("Doc1");
printer.addJob("Doc2");
console.log(printer.processJob()); // Doc1

This mimics how OS-level print queues work in Windows, Mac, and Linux.

Combining Stacks and Queues

Some systems use both together. For example:

  • A messaging app may use a queue for processing messages but a stack for managing chat undo history.
  • A compiler uses stacks for parsing nested expressions and queues for scheduling tasks.

Developer Takeaway

Stacks and Queues are not just theoretical they appear in daily applications.

  • Stacks: Undo/Redo, browser back button, call stack, expression parsing
  • Queues: Task scheduling, print systems, messaging apps, load balancing

Understanding them makes you a stronger developer and also helps explain your thought process in interviews with real-world analogies.

Frequently Asked Questions

Q1. Which is faster: Stack or Queue?
Both are efficient with O(1) push/pop operations, but the use case defines which one is better.

Q2. Can I implement a queue using stacks?
Yes, you can simulate a queue with two stacks. This is a common interview problem.

Q3. Where are stacks used in programming languages?
Every function call in Java, Python, or JavaScript uses the call stack internally.

Q4. Where are queues used in web development?
They power background jobs, message queues, and load balancers.

Q5. How do I practice Stacks and Queues?
Build features like undo-redo, task processors, or print queues in your projects.