Space Complexity of Queue Operations


Space complexity refers to the amount of extra memory used by an operation relative to the input size. In queues, most operations are performed in-place, which means they operate directly within the existing structure without requiring additional memory. Below, we explore the space complexity of key queue operations: Enqueue, Dequeue, Peek, isEmpty, and Size.

Summary Table

Operation Space Complexity Why?
Enqueue O(1) Element is added to the rear using constant space
Dequeue O(1) Element is removed from the front without new memory
Peek O(1) Accesses the front element directly
isEmpty O(1) Checks size or front/rear pointer
Size O(1) Returns a maintained count variable

1. Enqueue

The enqueue operation inserts a new element at the rear of the queue.

2. Dequeue

The dequeue operation removes the element from the front of the queue.

3. Peek

The peek operation retrieves the front element without removing it.

4. isEmpty

The isEmpty operation checks if the queue is empty.

5. Size

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