FIFO behavior
The first element enqueued is the first element dequeued. New values never skip ahead of values already waiting in the queue.
A queue is a First-In, First-Out (FIFO) data structure. New elements enter at the rear, while existing elements leave from the front. The element that has waited the longest is removed first.
Enter a value and choose an operation.
enqueue(value):
if queue is full:
report overflow
else:
add value at rear
rear = rear + 1
The first element enqueued is the first element dequeued. New values never skip ahead of values already waiting in the queue.
The front identifies the next removable element. The rear identifies where the next new element will be inserted.
Overflow occurs when Enqueue is attempted on a full fixed-capacity queue. Underflow occurs when Dequeue or Front is attempted on an empty queue.
Queues are used in task scheduling, print processing, message buffering, breadth-first search, request handling, and event systems.
| Operation | Time | Reason |
|---|---|---|
| Enqueue | O(1) | The new value is added directly at the rear. |
| Dequeue | O(1) | The front reference advances to the next element. |
| Front / Rear | O(1) | The boundary values are read directly. |
| Search | O(n) | Every queued value may need to be inspected. |
| Space | O(n) | Storage grows with the number of queued values. |