data-structures

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:

  1. : Adds element to the back of the queue.
  2. : Removes and returns the element at the front of the queue.
  3. : 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

OperationTime Complexity