Circular Linked List Implementation of Circular Queue
Last Updated :
05 Dec, 2024
The task is to implement the circular queue with the following operations using a circular linked list.
Operations on Circular Queue:
- Front: Get the front item from the queue.
- Rear: Get the last item from the queue.
- enQueue(value): This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at the Rear position.
- deQueue(): This function is used to delete an element from the circular queue. In a queue, the element is always deleted from the front position.
Prerequisite – Circular Singly Linked List

Approach:
We have discussed basics and how to implement circular queue using array. Please refer to Introduction to Circular Queue.
The idea is to initialize two pointers front and rear. front will point to the first node of the circular linked list, and rear will point to the last node of the circular linked list.
Operations:
- Front(): return the front node’s value if not null. Otherwise return -1.
- Rear(): return the rear node’s value if not null. Otherwise return -1.
- EnQueue(int value): Create a new node and set its value. If front is null, then set front = rear = newNode. Otherwise, set tail->next = newNode, tail = newNode, tail->next = front.
- DeQueue(): If list is empty, return -1. Otherwise get the front node’s value and remove it from the list. If the list contains only one node, then set front = rear = null. Otherwise update head = head->next and rear->next = head.
C++
// C++ program for insertion and
// deletion in Circular Queue
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int newdata) {
data = newdata;
next = nullptr;
}
};
class Queue {
public:
Node *front, *rear;
Queue() {
front = rear = nullptr;
}
};
// Function to insert element in a Circular queue
void enQueue(Queue *q, int value) {
struct Node *newNode = new Node(value);
if (q->front == nullptr)
q->front = newNode;
else
q->rear->next = newNode;
q->rear = newNode;
q->rear->next = q->front;
}
// Function to delete element from Circular Queue
int deQueue(Queue *q) {
// if queue is empty
if (q->front == nullptr) {
return -1;
}
int value;
// If this is the last node to be deleted
if (q->front == q->rear) {
value = q->front->data;
delete (q->front);
q->front = nullptr;
q->rear = nullptr;
}
else {
Node *temp = q->front;
value = temp->data;
q->front = q->front->next;
q->rear->next = q->front;
delete (temp);
}
return value;
}
// Function to return the front value
int front(Queue *q) {
Node *front = q->front;
if (front == nullptr) {
return -1;
}
return front->data;
}
// Function to return the rear value
int rear(Queue *q) {
Node *rear = q->rear;
if (rear == nullptr) {
return -1;
}
return rear->data;
}
void printQueue(Queue *q) {
Node *curr = q->front;
do {
cout << curr->data << " ";
curr = curr->next;
} while (curr != q->front);
cout << endl;
}
int main() {
Queue *q = new Queue;
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
enQueue(q, 20);
cout << "Front value: " << front(q) << endl;
cout << "Rear value: " << rear(q) << endl;
printQueue(q);
cout << "Deleted value = " << deQueue(q) << endl;
cout << "Deleted value = " << deQueue(q) << endl;
printQueue(q);
return 0;
}
C
// C program for insertion and deletion
// in Circular Queue
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
struct Node* createNode(int newdata);
// Function to insert element in a Circular queue
void enQueue(struct Queue* q, int value) {
struct Node* newNode = createNode(value);
if (q->front == NULL)
q->front = newNode;
else
q->rear->next = newNode;
q->rear = newNode;
q->rear->next = q->front;
}
// Function to delete element from Circular Queue
int deQueue(struct Queue* q) {
// if queue is empty
if (q->front == NULL) {
return -1;
}
int value;
// If this is the last node to be deleted
if (q->front == q->rear) {
value = q->front->data;
free(q->front);
q->front = q->rear = NULL;
} else {
struct Node* temp = q->front;
value = temp->data;
q->front = q->front->next;
q->rear->next = q->front;
free(temp);
}
return value;
}
void printQueue(struct Queue* q) {
if (q->front == NULL) return;
struct Node* curr = q->front;
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != q->front);
printf("\n");
}
// Function to return the front value
int front(struct Queue* q) {
struct Node* front = q->front;
if (front == NULL) {
return -1;
}
return front->data;
}
// Function to return the rear value
int rear(struct Queue* q) {
struct Node* rear = q->rear;
if (rear == NULL) {
return -1;
}
return rear->data;
}
struct Queue* createQueue() {
struct Queue* q =
(struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
struct Node* createNode(int newdata) {
struct Node* newnode
= (struct Node*)malloc(sizeof(struct Node));
newnode->data = newdata;
newnode->next = NULL;
return newnode;
}
int main() {
struct Queue* q = createQueue();
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
enQueue(q, 20);
printf("Front value: %d\n", front(q));
printf("Rear value: %d\n", rear(q));
printQueue(q);
printf("Deleted value = %d\n", deQueue(q));
printf("Deleted value = %d\n", deQueue(q));
printQueue(q);
return 0;
}
Java
// Java program for insertion and deletion
// in Circular Queue
class Node {
int data;
Node next;
Node(int newdata) {
data = newdata;
next = null;
}
}
class Queue {
Node front, rear;
Queue() {
front = rear = null;
}
}
class GfG {
// Function to insert element in a Circular queue
static void enQueue(Queue q, int value) {
Node newNode = new Node(value);
if (q.front == null)
q.front = newNode;
else
q.rear.next = newNode;
q.rear = newNode;
q.rear.next = q.front;
}
// Function to delete element from Circular Queue
static int deQueue(Queue q) {
if (q.front == null)
return -1;
int value;
if (q.front == q.rear) {
value = q.front.data;
q.front = q.rear = null;
} else {
Node temp = q.front;
value = temp.data;
q.front = q.front.next;
q.rear.next = q.front;
}
return value;
}
// Function to return the front value
static int front(Queue q) {
Node front = q.front;
if (front == null) {
return -1;
}
return front.data;
}
// Function to return the rear value
static int rear(Queue q) {
Node rear = q.rear;
if (rear == null) {
return -1;
}
return rear.data;
}
static void printQueue(Queue q) {
Node curr = q.front;
if (curr == null) return;
do {
System.out.print(curr.data + " ");
curr = curr.next;
} while (curr != q.front);
System.out.println();
}
public static void main(String[] args) {
Queue q = new Queue();
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
enQueue(q, 20);
System.out.println("Front value = " + front(q));
System.out.println("Rear value = " + rear(q));
printQueue(q);
System.out.println("Deleted value = " + deQueue(q));
System.out.println("Deleted value = " + deQueue(q));
printQueue(q);
}
}
Python
# Python program for insertion and deletion
# in Circular Queue
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
class Queue:
def __init__(self):
self.front = self.rear = None
# Function to insert element in a Circular queue
def enQueue(q, value):
newNode = Node(value)
if q.front is None:
q.front = newNode
else:
q.rear.next = newNode
q.rear = newNode
q.rear.next = q.front
# Function to delete element from
# Circular Queue
def deQueue(q):
if q.front is None:
return -1
value = None
if q.front == q.rear:
value = q.front.data
q.front = q.rear = None
else:
temp = q.front
value = temp.data
q.front = q.front.next
q.rear.next = q.front
return value
# Function to return the front value
def front(q):
front = q.front
if front is None:
return -1
return front.data
# Function to return the rear value
def rear(q):
rear = q.rear
if rear is None:
return -1
return rear.data
def printQueue(q):
if q.front is None:
return
curr = q.front
while True:
print(curr.data, end=" ")
curr = curr.next
if curr == q.front:
break
print()
if __name__ == "__main__":
q = Queue()
enQueue(q, 14)
enQueue(q, 22)
enQueue(q, 6)
enQueue(q, 20)
print("Front value =", front(q))
print("Rear value =", rear(q))
printQueue(q)
print("Deleted value =", deQueue(q))
print("Deleted value =", deQueue(q))
printQueue(q)
C#
// C# program for insertion and deletion
// in Circular Queue
using System;
class Node {
public int data;
public Node next;
public Node(int newData) {
data = newData;
next = null;
}
}
class Queue {
public Node front, rear;
public Queue() {
front = rear = null;
}
}
class GfG {
// Function to insert element in a Circular queue
static void EnQueue(Queue q, int value) {
Node newNode = new Node(value);
if (q.front == null)
q.front = newNode;
else
q.rear.next = newNode;
q.rear = newNode;
q.rear.next = q.front;
}
// Function to delete element from
// Circular Queue
static int DeQueue(Queue q) {
if (q.front == null)
return -1;
int value;
if (q.front == q.rear) {
value = q.front.data;
q.front = q.rear = null;
} else {
Node temp = q.front;
value = temp.data;
q.front = q.front.next;
q.rear.next = q.front;
}
return value;
}
// Function to return the front value
static int Front(Queue q) {
Node front = q.front;
if (front == null) {
return -1;
}
return front.data;
}
// Function to return the rear value
static int Rear(Queue q) {
Node rear = q.rear;
if (rear == null) {
return -1;
}
return rear.data;
}
static void PrintQueue(Queue q) {
if (q.front == null) return;
Node curr = q.front;
do {
Console.Write(curr.data + " ");
curr = curr.next;
} while (curr != q.front);
Console.WriteLine();
}
static void Main(string[] args) {
Queue q = new Queue();
EnQueue(q, 14);
EnQueue(q, 22);
EnQueue(q, 6);
EnQueue(q, 20);
Console.WriteLine("Front value = " + Front(q));
Console.WriteLine("Rear value = " + Rear(q));
PrintQueue(q);
Console.WriteLine("Deleted value = " + DeQueue(q));
Console.WriteLine("Deleted value = " + DeQueue(q));
PrintQueue(q);
}
}
JavaScript
// JavaScript program for insertion and
// deletion in Circular Queue
class Node {
constructor(newData) {
this.data = newData;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
}
}
// Function to insert element in
// a Circular queue
function enQueue(q, value) {
let newNode = new Node(value);
if (q.front === null)
q.front = newNode;
else
q.rear.next = newNode;
q.rear = newNode;
q.rear.next = q.front;
}
// Function to delete element from
// Circular Queue
function deQueue(q) {
if (q.front === null) return -1;
let value;
if (q.front === q.rear) {
value = q.front.data;
q.front = q.rear = null;
} else {
let temp = q.front;
value = temp.data;
q.front = q.front.next;
q.rear.next = q.front;
}
return value;
}
// Function to return the front value
function front(q) {
let front = q.front;
if (front == null) {
return -1;
}
return front.data;
}
// Function to return the rear value
function rear(q) {
let rear = q.rear;
if (rear == null) {
return -1;
}
return rear.data;
}
function printQueue(q) {
if (q.front === null) return;
let curr = q.front;
do {
console.log(curr.data);
curr = curr.next;
} while (curr !== q.front);
}
let q = new Queue();
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
enQueue(q, 20);
console.log("Front value = " + front(q));
console.log("Rear value = " + rear(q));
printQueue(q);
console.log("Deleted value = " + deQueue(q));
console.log("Deleted value = " + deQueue(q));
printQueue(q);
OutputFront value: 14
Rear value: 20
14 22 6 20
Deleted value = 14
Deleted value = 22
6 20
Time Complexity: O(1), for enQueue, deQueue, front, rear and O(n) for printQueue.
Auxiliary Space: O(n), where n is the maximum number of elements that can be stored in the queue.
Similar Reads
Circular Array Implementation of Queue
A Circular Queue is a way of implementing a normal queue where the last element of the queue is connected to the first element of the queue forming a circle. The operations are performed based on the FIFO (First In First Out) principle. It is also called 'Ring Buffer'. In a normal Queue, we can inse
8 min read
Queue - Linked List Implementation
In this article, the Linked List implementation of the queue data structure is discussed and implemented. Print '-1' if the queue is empty. Approach: To solve the problem follow the below idea: we maintain two pointers, front and rear. The front points to the first item of the queue and rear points
8 min read
Implementation of Deque using doubly linked list
A Deque (Double-Ended Queue) is a data structure that allows adding and removing elements from both the front and rear ends. Using a doubly linked list to implement a deque makes these operations very efficient, as each node in the list has pointers to both the previous and next nodes. This means we
9 min read
Next Greater Element in a Circular Linked List
Given a circular singly linked list, the task is to print the next greater element for each node in the linked list. If there is no next greater element for any node, then print "-1" for that node. Examples: Input: head = 1 ? 5 ? 2 ? 10 ? 0 ? (head)Output: 5 10 10 -1 -1Explanation:The next greater e
11 min read
Introduction to Circular Doubly Linked List
A circular doubly linked list is defined as a circular linked list in which each node has two links connecting it to the previous node and the next node. Characteristics of Circular Doubly Linked List :A circular doubly linked list has the following properties: Circular: A circular doubly linked lis
4 min read
Introduction and Array Implementation of Queue
Similar to Stack, Queue is a linear data structure that follows a particular order in which the operations are performed for storing data. The order is First In First Out (FIFO). One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginn
2 min read
Implementation of Queue using Linked List in Python
A queue is a linear data structure that follows the FIFO (First In First Out) principle. The first element added is the first one to be removed. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that ca
4 min read
Introduction to Circular Linked List
A circular linked list is a data structure where the last node connects back to the first, forming a loop. This structure allows for continuous traversal without any interruptions. Circular linked lists are especially helpful for tasks like scheduling and managing playlists, allowing for smooth navi
15+ min read
Search an Element in Doubly Circular Linked List
Pre-requisite: Convert an Array to a Circular Doubly Linked List, Doubly Circular Linked ListGiven a doubly circular linked list. The task is to find the position of an element in the list. Image Representation: Algorithm: Declare a temp pointer, and initialize it to the head of the list.Iterate the
13 min read
What is Circular Queue | Circular Queue meaning
A circular queue is an extended version of regular queue in which the last element of the queue is connected to the first element of the queue forming a cycle. Properties of Circular Queue: Along with the properties of a regular queue the circular queue has som other unique properties as mentioned b
4 min read