What is a Queue?
A queue is a FIFO data structure where the first item added is the first item removed, like a line at a store.
A queue is a data structure that follows First-In-First-Out (FIFO) — like a line at a store. The first person in line is the first person served. New people join at the back, and people leave from the front.
Operations:
- Enqueue: Add an item to the back (rear)
- Dequeue: Remove and return the front item
- Front/Peek: Look at the front item without removing it
- IsEmpty: Check if the queue is empty
Types:
- Simple queue: Basic FIFO
- Priority queue: Items with higher priority go first
- Circular queue: Wraps around when full
- Double-ended queue (Deque): Can add/remove from both ends
Common Uses:
- Task scheduling: Process tasks in order
- Message queues: Handle messages between services
- Breadth-first search: Explore graph level by level
- Print queues: Print documents in order
FAQ
What's the difference between a queue and a stack?
Queue is FIFO (first in, first out). Stack is LIFO (last in, first out).
What's a priority queue?
A queue where items have priorities. Higher priority items are served first, even if they arrived later.