Enqueue
Overview
Enqueue is the fundamental operation of a Queue that adds an element to the rear (end) of the queue. It preserves the First In, First Out (FIFO) ordering by ensuring new elements join the line at the back.
What Does Enqueue Do?
The enqueue operation:
- Inserts a new element
- Places it at the rear of the queue
- Updates internal pointers or indices
No existing elements change their relative order.
How It Works
Depending on the implementation:
Array-based Queue
- Insert at the rear index
- Increment the rear pointer
- May wrap around in a circular queue
Linked-list-based Queue
- Create a new node
- Link it after the current rear
- Update the rear reference
Example
Pseudocode
enqueue(queue, value):
if queue is full:
error "Queue Overflow"
rear = rear + 1
queue[rear] = value
Real-world Analogy
Enqueue is like joining the end of a line. You don’t cut in, you take your place at the back 🚶♂️➡️🚶♀️.
Time Complexity
- Time: O(1)
- Space: O(1) per operation
Error Conditions
- Queue overflow: when the queue is full (in fixed-size implementations)
Use Cases
- Adding tasks to a scheduler
- Receiving network packets
- Job submission systems
- Event handling pipelines
Advantages
- Fast insertion
- Preserves ordering
- Simple operation
Limitations
- Cannot insert at arbitrary positions
- Depends on queue capacity (if fixed)