DEV Community

Cover image for 📦 Stacks & Queues: Two Sides of the Same Coin
OneDev
OneDev

Posted on

📦 Stacks & Queues: Two Sides of the Same Coin

When it comes to data structures, Stacks and Queues are two of the simplest — and most powerful — tools in your problem-solving toolkit.

They're like the behind-the-scenes stagehands that quietly manage order, timing, and flow in countless algorithms and real-world systems.

Let’s take a look.


🥞 Stack — Last In, First Out (LIFO)

Imagine a stack of plates. You can only take the top plate off the stack, and you can only add new ones to the top.

That’s how a Stack works:

  • Push → add to the top
  • Pop → remove from the top
  • Peek → look at the top without removing it

Real-world examples:

  • Undo/Redo functionality
  • Navigation History (Browser or App Screens)
  • Expression Evaluation & Parsing
  • Backtracking Algorithms

Example in React Native Context

React Navigation’s stack navigator (screen navigation stack)

Code Example (JavaScript):

const stack = [];
stack.push(1);
stack.push(2);
console.log(stack.pop()); // 2
console.log(stack.pop()); // 1
Enter fullscreen mode Exit fullscreen mode

🚶 Queue — First In, First Out (FIFO)

Now picture a line at the coffee shop. The first person to enter is the first one to get served.

That’s the idea of a Queue:

  • Enqueue → add to the end
  • Dequeue → remove from the front
  • Peek → look at the front without removing

Real-world examples:

  • Task scheduling
  • Message Queues
  • Print Queue
  • Event Handling (UI events like clicks or keystrokes)

Example in React Native Context:

Offline message sending queue in a chat app — messages are queued if the user is offline, then sent one by one when connection is restored

Code Example (JavaScript):

const queue = [];
queue.push('A');
queue.push('B');
console.log(queue.shift()); // 'A'
console.log(queue.shift()); // 'B'
Enter fullscreen mode Exit fullscreen mode

🔁 Same Concept, Different Languages

While the concept of stacks and queues remains consistent, each programming language has its own way of implementing them:

Language Stack Queue
JavaScript Array.push() / .pop() Array.push() / .shift()
Python list, collections.deque collections.deque, queue.Queue
Java Stack, Deque Queue, LinkedList, PriorityQueue

Java provides dedicated, type-safe classes out of the box — ideal for enterprise-scale apps.

Python hits the sweet spot with easy syntax and optimized utilities like deque.

🧪 JavaScript keeps it simple — arrays get the job done, but aren’t true queues or stacks under the hood.

👉 If you want to know how to create proper stacks and queues in JavaScript step by step, let me know in the comments — happy to make a follow-up!


🧠 TL;DR

  • Stacks = LIFO (Last In, First Out)
  • Queues = FIFO (First In, First Out)
  • They're used everywhere, from compilers to servers
  • You can implement both using arrays, but some languages give you more powerful tools

✌️ That’s a Wrap!

These might be simple structures, but don’t underestimate them. They’re building blocks for everything from graph traversals to memory management.

Got questions or want a deeper dive into how they’re used in algorithms? Drop a comment!

Top comments (0)