Queue Operations


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

{ "array": ["10", "20", "30"], "emptyIndices": [3, 4] }

Enqueue 40 to the rear

{ "array": ["10", "20", "30", "40"], "highlightIndicesGreen": [3] }

Updated Queue

{ "array": ["10", "20", "30", "40"], "emptyIndices": [4] }

2. Dequeue

The dequeue operation removes the front element from the queue.

Initial Queue

{ "array": ["10", "20", "30", "40"], "emptyIndices": [4] }

Dequeue 10 from the front

{ "array": ["10", "20", "30", "40"], "highlightIndicesRed": [0] }

Updated Queue

{ "array": ["20", "30", "40"] }

3. Peek

The peek operation returns the front element of the queue without removing it.

Current Queue:

{ "array": [10, 20, 30], "highlightIndices": [0] }

Peek Result: 10

4. isEmpty

The isEmpty operation checks whether the queue has any elements.

Queue with elements:

{ "array": [10, 20, 30], "highlightIndices": [] }

isEmpty Result: false

Empty Queue:

{ "array": [""], "emptyIndices": [0], "highlightIndices": [] }

isEmpty Result: true

5. Size

The size operation returns the number of elements currently present in the queue.

Queue with 3 elements:

{ "array": [10, 20, 30], "highlightIndices": [] }

Size Result: 3

Queue with 0 elements:

{ "array": [""], "emptyIndices": [0], "highlightIndices": [] }

Size Result: 0