There are four main types of queues: Simple Queue (basic FIFO), Circular Queue (wraps around to reuse space), Priority Queue (elements are processed by importance, not arrival order), and Deque (Double Ended Queue — insert/delete from both ends).
Simple Queue: A grocery store checkout line. Circular Queue: A lazy susan at a restaurant — food goes around and around, no wasted space. Priority Queue: An ER waiting room — a heart attack patient (high priority) is seen before someone with a cold, even if the cold patient arrived first. Deque: A deck of cards where you can draw from the top or the bottom.
The basic line. Can waste space.
The line wraps around in a circle. No space wasted.
Important people cut the line! (Sorted by priority).
Join or leave from BOTH ends.
Watch how the pointers wrap around when they reach the end!
1// Priority Queue: Higher number = higher priority2// Unlike regular queue, we INSERT in sorted order3void priorityEnqueue(int value, int priority) {4// Find the right position based on priority5// Shift elements if needed6// Place the new element78// Dequeue still takes from Front — O(1)9// But Enqueue may need to search — O(n) for array10// Using a Heap makes both O(log n)!11}
The Four Types of Queues
1. Simple Queue (Linear Queue)
The basic queue we've been studying. Follow FIFO strictly. The problem: once Rear reaches the end of the array, even if there are empty spots at the front, you can't enqueue. This leads to the "False Overflow" we discussed.
2. Circular Queue (The Lazy Susan)
Connects the end of the array back to the beginning using modulo arithmetic: rear = (rear + 1) % SIZE. This eliminates wasted space entirely.
(rear + 1) % SIZE == frontfront == -13. Priority Queue (The VIP Line)
Not strictly FIFO! Elements are assigned a priority, and the highest-priority element is dequeued first, regardless of when it arrived.
4. Deque (Double Ended Queue)
Allows insertion and deletion from BOTH ends — front and rear.
Different problems need different queue behaviors. CPU scheduling uses priority queues (important tasks first). Circular buffers power audio/video streaming. Deques are used in algorithms like sliding window problems. Knowing which type to pick is a fundamental design skill.
"What advantage does a Circular Queue have over a Simple Queue?"
A Circular Queue reuses empty slots left behind by dequeue operations by wrapping the Rear pointer back to the beginning using modulo arithmetic. A Simple Queue wastes those slots.