Circular Queue using Linked List in Java
Circular Queue using Linked List
Circular Queue using Linked List in Java is a dynamic implementation of a circular queue where nodes are connected in a circular manner.
Unlike an array based circular queue (which has fixed capacity), a linked list circular queue grows dynamically and avoids overflow until memory is exhausted.
What is a Circular Queue using Linked List?
In a normal linked list queue:
front → 10 → 20 → 30 → null
In a circular linked list queue, the last node points back to the first node:
front → 10 → 20 → 30
↑ ↓
← ← ← ← ←The following are the operations that can be performed on a circular queue:
- Front: It is used to get the front item from the Queue.
- Rear: It is used to get the last element from the Queue.
- enQueue() This operation adds a new node after rear and moves rear to the next node.
- deQueue() This operation removes the front node and moves front to the next node.
Why Use Circular Queue with Linked List?
| Feature | Circular Array | Circular Linked List |
|---|---|---|
| Size | Fixed | Dynamic |
| Overflow | Yes | No (until memory full) |
| Memory Use | Continuous | Non-contiguous |
| Flexibility | Medium | High |
Use it when:
- Queue size is unpredictable
- Circular traversal is required
- Memory flexibility is needed
Algorithm for Implementation of Circular Queue using Linked List
1. Create new node
2. If queue is empty:
front = rear = newNode
rear.next = front
3. Else:
rear.next = newNode
rear = newNode
rear.next = front
1. If queue is empty:
Print "Queue is empty"
Return -1
2. If front == rear (only one element):
value = front.data
front = rear = null
Return value
3. Else:
value = front.data
front = front.next
rear.next = front
Return value
1. If queue is empty:
Return -1
2. Else:
Return front.data
Return front == null
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Java Code for Circular Queue using Linked List
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class CircularQueueLinkedList {
private Node front;
private Node rear;
public CircularQueueLinkedList() {
front = rear = null;
}
// Check if queue is empty
public boolean isEmpty() {
return front == null;
}
// Enqueue operation
public void enqueue(int value) {
Node newNode = new Node(value);
if (isEmpty()) {
front = rear = newNode;
rear.next = front; // circular link
} else {
rear.next = newNode;
rear = newNode;
rear.next = front;
}
System.out.println(value + " inserted");
}
// Dequeue operation
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty (Underflow)");
return -1;
}
int value;
// If only one element
if (front == rear) {
value = front.data;
front = rear = null;
} else {
value = front.data;
front = front.next;
rear.next = front;
}
return value;
}
// Peek operation
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
return front.data;
}
// Display queue
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
System.out.print("Circular Queue: ");
Node temp = front;
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != front);
System.out.println();
}
// Main method
public static void main(String[] args) {
CircularQueueLinkedList queue = new CircularQueueLinkedList();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
System.out.println("Removed: " + queue.dequeue());
System.out.println("Removed: " + queue.dequeue());
queue.display();
queue.enqueue(40);
queue.enqueue(50);
queue.display();
System.out.println("Front element: " + queue.peek());
System.out.println("Removed: " + queue.dequeue());
System.out.println("Removed: " + queue.dequeue());
System.out.println("Removed: " + queue.dequeue());
queue.display(); // underflow case
}
}
Input:
enqueue(10)
enqueue(20)
enqueue(30)
dequeue()
dequeue()
enqueue(40)
enqueue(50)
dequeue()
dequeue()
dequeue()
Output:
10 inserted
20 inserted
30 inserted
Circular Queue: 10 20 30
Removed: 10
Removed: 20
Circular Queue: 30
40 inserted
50 inserted
Circular Queue: 30 40 50
Front element: 30
Removed: 30
Removed: 40
Removed: 50
Queue is empty
Space Complexity: O(n)
Note:
In some cases like:
- Empty queue:
front == null → prevent dequeue and peek - Single element case:
After deletion, set both front and rear to null - Circular link maintenance:
Always update rear.next = front
Frequently Asked Questions
Answer:
It is a queue where the last node connects back to the first node, forming a circular structure.
Answer:
Enqueue and dequeue operations take O(1) time.
Answer:
No fixed overflow, it grows dynamically until memory is exhausted.
Answer:
Array version has fixed size; linked list version is dynamic.
Answer:
To preserve the circular structure of the queue.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java

Login/Signup to comment