VOOZH about

URL: https://www.geeksforgeeks.org/dsa/queue-linked-list-implementation/

⇱ Queue - Linked List Implementation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Queue - Linked List Implementation

Last Updated : 20 Sep, 2025

A Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. The element inserted first is the first one to be removed.
πŸ‘ Queue-Linked-List-Implementation_

It can be implemented using a linked list, where each element of the queue is represented as a node.

Declaration of Queue using Linked List

To implement a queue with a linked list, we maintain:

A Node structure/class that contains:

  • data β†’ to store the element.
  • next β†’ pointer/reference to the next node in the queue.

Two pointers/references:

  • front β†’ points to the first node (head of the queue).
  • rear β†’ points to the last node (tail of the queue).

Operations on Queue using Linked List:

Enqueue Operation

The enqueue operation adds an element to the rear of the queue. Unlike array implementation, there is no fixed capacity in linked list. Overflow occurs only when memory is exhausted.

  • A new node is created with the given value.
  • If the queue is empty (front == null and rear == null), both front and rear are set to this new node.
  • Otherwise, the current rear’s next pointer is set to the new node.
  • The rear pointer is updated to point to the new node.

Time Complexity: O(1)
Auxiliary Space: O(1)

Dequeue Operation

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

  • If the queue is empty (front == null), return underflow (queue is empty).
  • Otherwise, store the current front node in a temporary pointer.
  • Move the front pointer to the next node (front = front.next).
  • If the front becomes null, also set rear = null (queue becomes empty).

Time Complexity: O(1)
Auxiliary Space: O(1)

πŸ‘ Queue-Linked-List-Implementation_

isEmpty Operation

The isEmpty operation checks whether the queue has no elements.

  • If the front pointer is NULL, it means the queue is empty β†’ return true.
  • Otherwise, the queue has elements β†’ return false.

Time Complexity: O(1)
Auxiliary Space: O(1)

Front Operation

The front() function returns the element at the front of the queue without removing it.

  • If the queue is empty (front == NULL), print a message and return -1.
  • Otherwise, return front->data (the value at the front).

Time Complexity: O(1)
Auxiliary Space: O(1)

Full Implementations of Queue Using Linked List


Output
Dequeue: 10
Front: 20
Size: 2

Related Article:

Array Implementation of Queue

Comment