What is a Single-Ended Queue?
A single-ended queue (often just called a queue) is a linear data structure that follows the FIFO (First-In-First-Out) principle. Elements are added (enqueued) at the rear and removed (dequeued) from the front, maintaining strict ordering.
Key Characteristics
Single-ended queues have these fundamental properties:
- Two ends:
- Front (for removal) and rear (for insertion)
- Basic Operations:
- enqueue() - Add to rear
- dequeue() - Remove from front
- peek() - View front element
- isEmpty() - Check if empty
- Fixed Order:
- Elements are processed in exact arrival sequence
Visual Example
Operation sequence on an initially empty queue:
- enqueue(10): [10]
- enqueue(20): [10, 20]
- enqueue(30): [10, 20, 30]
- dequeue(): Returns 10 → [20, 30]
- peek(): Returns 20 → [20, 30] (unchanged)
Implementation Variations
Common implementation approaches:
- Array-Based:
- Fixed or dynamic array
- Need to handle wrap-around for circular queues
- Linked List:
- Head pointer as front
- Tail pointer as rear
- Efficient O(1) operations
Time Complexity
- enqueue(): O(1)
- dequeue(): O(1)
- peek(): O(1)
- isEmpty(): O(1)
Applications
Single-ended queues are used in:
- CPU task scheduling
- Print job management
- Breadth-First Search (BFS) algorithms
- Buffering data streams
- Handling requests in web servers
Comparison with Double-Ended Queue
Key differences:
- Single-ended only allows insertion at rear and removal at front
- Double-ended (deque) allows insertion/removal at both ends
- Single-ended has stricter FIFO enforcement
- Single-ended is simpler to implement
The single-ended queue is a fundamental data structure in computer science, providing predictable ordering that's essential for many algorithms and system design patterns where processing order matters.