Queues support a core set of operations that are fundamental to their First-In First-Out (FIFO) behavior. In this section, we explore the essential operations — Enqueue, Dequeue, Peek, isEmpty, Size, and Traversal — using visual step-by-step diagrams.
1. Enqueue
The enqueue operation adds a new element to the rear (end) of the queue.
Initial Queue
Enqueue 40 to the rear
Updated Queue
2. Dequeue
The dequeue operation removes the front element from the queue.
Initial Queue
Dequeue 10 from the front
Updated Queue
3. Peek
The peek operation returns the front element of the queue without removing it.
Current Queue:
Peek Result: 10
4. isEmpty
The isEmpty operation checks whether the queue has any elements.
Queue with elements:
isEmpty Result: false
Empty Queue:
isEmpty Result: true
5. Size
The size operation returns the number of elements currently present in the queue.
Queue with 3 elements:
Size Result: 3
Queue with 0 elements:
Size Result: 0