queue.definition
queue.descriptionPart1
queue.descriptionPart2
queue.keyPointsTitle
- queue.keyPoint1
- queue.keyPoint2
- queue.keyPoint3
- queue.keyPoint4
queue.exampleTitle
enqueue(40) → queue.addedToRearqueue.syntaxTitle
// Java
Queue<Integer> queue = new LinkedList<>();
queue.offer(10); // enqueue
int front = queue.poll(); // dequeue# Python
from collections import deque
queue = deque()
queue.append(10) # enqueue
front = queue.popleft() # dequeue// C++
#include <queue>
std::queue<int> q;
q.push(10); // enqueue
int front = q.front(); q.pop(); // dequeue// JavaScript
const queue = [];
queue.push(10); // enqueue
const front = queue.shift(); // dequeueInteractive Queue Visualization
enhanced.realWorldExamples.title
Streaming services queue video chunks for smooth playback.
Why? FIFO ensures frames play in correct order.
Printers queue documents in order of submission.
Why? First-come-first-served scheduling is fair and predictable.
Time Complexity
This linear queue uses an array with shifting on dequeue. While enqueue remains O(1), dequeue requires shifting all elements left, making it O(n). This trade-off keeps the front always at index 0.
| Operation | Time | Why? |
|---|---|---|
| Enqueue | O(1) | Add element to rear (end of array) |
| Dequeue | O(n) | Remove front element, then shift all remaining elements left |
| Peek / Front | O(1) | View front element (index 0) without removing |
| isEmpty | O(1) | Check if queue is empty |
| isFull | O(1) | Check if queue has reached capacity |
| Size | O(1) | Get number of elements |
| Clear | O(n) | Remove all elements |
enhanced.complexityAnalysis.title
enhanced.complexityAnalysis.mathematicalTitle
Enqueue and dequeue are O(1) when implemented with linked list or circular array, as they only modify head/tail pointers.
🟢Best Case
O(1) for enqueue/dequeue operations in all cases.
🟡Average Case
O(1) for standard operations. Linear search for specific element is O(n).
🔴Worst Case
Array-based queues may need O(n) for resizing. Dequeue from array without circular implementation is O(n).
💾Space Complexity
O(n) for n elements stored in the queue.
Continue Learning
Test your knowledge with a quiz or explore implementation code in multiple languages.