Custom Queue

About

A Queue follows the FIFO (First In, First Out) principle, meaning the first element added is the first one removed.

1. LinkedList-based Queue

For a linked list-based queue, we maintain:

  • A front pointer (points to the first element)

  • A rear pointer (points to the last element)

Node Class

Each element in the queue is stored in a Node, which contains:

  • data: The value of the node

  • next: Pointer to the next node

class Node<E> {
    E data;
    Node<E> next;

    Node(E data) {
        this.data = data;
        this.next = null;
    }
}

Queue Class

Core Operations

Enqueue (Add an element to the rear)

Dequeue (Remove and return the front element)

Peek (View the front element without removing it)

Check if Queue is Empty

Get Queue Size

Print Queue

Complete Custom Queue Implementation

Time Complexity Analysis

Operation
Complexity

Enqueue (enqueue)

O(1)

Dequeue (dequeue)

O(1)

Peek (peek)

O(1)

Is Empty (isEmpty)

O(1)

Get Size (size)

O(1)

Print Queue (printQueue)

O(n)

2. Array-based Queue

Queue Data Structure

For an array-based queue, we maintain:

  • A front pointer (points to the first element)

  • A rear pointer (points to the last element)

  • A size variable to track the number of elements

  • A fixed-size array to store elements

Queue Class Definition

  • The queue is initialized with a fixed size.

  • front points to the first element.

  • rear tracks the last inserted element.

  • size keeps track of the number of elements.

Core Operations

Enqueue (Add an element at the rear)

Dequeue (Remove and return the front element)

Peek (View the front element without removing it)

Check if Queue is Empty

Check if Queue is Full

Get Queue Size

Print Queue

Complete Custom Queue Implementation

Time Complexity Analysis

Operation
Complexity

Enqueue (enqueue)

O(1)

Dequeue (dequeue)

O(1)

Peek (peek)

O(1)

Is Empty (isEmpty)

O(1)

Is Full (isFull)

O(1)

Get Size (size)

O(1)

Print Queue (printQueue)

O(n)

Last updated