Definition
Queue
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. Elements are added to the back (rear) and removed from the front.
It provides three primary operations:
- : Adds element to the back of the queue.
- : Removes and returns the element at the front of the queue.
- : Returns the element at the front without removing it.
Visual Representation
A queue can be conceptualised as a sequence of elements where insertion and deletion happen at opposite ends:
Implementation
Array-based (Circular Buffer)
Using a fixed-size array, the queue uses two pointers, front and back. To avoid wasting space, the indices wrap around using modulo arithmetic:
Linked List
Using a singly linked list with a tail pointer:
- is an operation (append to tail).
- is an operation (remove from head).
Complexity
| Operation | Time Complexity |
|---|---|