Queue related data structure

1) FIFO Queue (the classic queue)

Rule:
First In → First Out

append back, pop from front

Like people waiting for coffee.

enter:  A B C D
leave:  A B C D

Operations:

enqueue  (add at back)
dequeue  (remove from front)

Rust implementation

Use:

VecDeque
use std::collections::VecDeque;

let mut q = VecDeque::new();
q.push_back("A");
q.push_back("B");
q.push_back("C");

q.pop_front(); // A

Why VecDeque?
Because a queue needs:

push_back + pop_front

exactly the operations a ring buffer optimizes.

Typical uses:

  • BFS graph traversal
  • job scheduler
  • async task queues
  • streaming data
  • sliding window algorithms

2) Double-Ended Queue (Deque)

This is actually what VecDeque really is.

You can add/remove from both ends:

push_front
push_back
pop_front
pop_back
<-- front       back -->
    A B C D

This is more powerful than a queue.

Typical uses:

  • sliding window max/min
  • undo/redo history
  • LRU cache
  • monotonic queue (very important algorithmic trick)

3) Stack (LIFO)

Rule:
Last In → First Out

Like plates in a stack.

enter:  A B C D
leave:  D C B A

Operations:

push
pop
peek

Rust:

let mut stack = Vec::new();
stack.push(1);
stack.push(2);
stack.pop(); // 2

Important:

In Rust, Vec is intentionally designed to be a stack.

Used in:

  • recursion simulation
  • DFS
  • parsers
  • compilers
  • expression evaluation
  • backtracking

5) Monotonic Queue (VERY important concept)

This one is subtle and extremely powerful.

It is a deque that maintains sorted order automatically.

Used for:

  • sliding window maximum
  • streaming analytics
  • time-series queries

Internally:

VecDeque + pop_back while smaller

This is why deques show up constantly in competitive programming and systems code.


6) Circular Queue (Ring Buffer)

This is what VecDeque is implementing under the hood.

Fixed memory reused:

tail wraps → beginning

Used in:

  • network drivers
  • TCP buffers
  • audio processing
  • lock-free channels

Rust also uses this idea inside async runtimes.


Big picture

All of these are variations of one idea:

StructureRemoval rule
Stacknewest first
Queueoldest first
Dequeeither end
Priority Queuehighest priority
Monotonic Queuehighest/lowest in window

They differ only in who gets to leave next.


A very useful intuition

Arrays (Vec) optimize:

accessing by position

Queues optimize:

accessing by time

That’s why queues appear whenever data is:

  • streaming
  • scheduled
  • buffered
  • windowed

Leave a Reply

Your email address will not be published. Required fields are marked *