What is a queue?
A queue follows first in, first out (FIFO): items join at the rear and leave from the front, like people waiting in line.
This page covers four types. A simple queue keeps items in fixed slots, so slots freed at the front are wasted. A circular queue wraps the rear back to the start so those slots are reused. A priority queue removes the highest-priority item first, not the oldest. A deque (double-ended queue) allows adding and removing at both ends.
Time complexity
| Operation | Time | Notes |
|---|---|---|
| Enqueue and dequeue (simple, circular) | O(1) | Only front or rear moves |
| Priority queue enqueue | O(n) or O(log n) | O(n) in a sorted list, O(log n) with a heap |
| Priority queue dequeue | O(1) | The highest priority is at the front |
| Deque insert or delete at either end | O(1) | Both ends are open |
Try it yourself
- In Simple, dequeue a few items and keep enqueuing until the queue reports full even though front slots are free.
- Switch to Circular and repeat to see the rear wrap around and reuse those slots.
- In Priority, enqueue items with different priorities and watch them sort.
- In Deque, insert and delete at the front and the rear.
Which queue should you pick?
Use a simple queue for strictly in-order work, a circular queue when the buffer has a fixed size and runs forever, a priority queue when some jobs matter more than others, and a deque when you need both ends, as in sliding-window problems.
Common questions
- What is the difference between a queue and a stack?
- A queue is first in, first out. A stack is last in, first out.
- Why use a circular queue?
- A simple array queue cannot reuse slots freed at the front. A circular queue wraps around, so it uses all of its slots.
- How does a priority queue differ from a normal queue?
- A normal queue serves items in arrival order. A priority queue serves the item with the highest priority first, whatever its arrival time.