Circular Queue | Set 2 (Circular Linked List Implementation)
Prerequisite – Circular Singly Linked List
We have discussed basics and how to implement circular queue using array in set 1.
Circular Queue | Set 1 (Introduction and Array Implementation)
In this post another method of circular queue implementation is discussed, using Circular Singly Linked List.
Operations on Circular Queue:
- Front:Get the front item from queue.
- Rear: Get the last item from 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 Rear position.
- Steps:
- Create a new node dynamically and insert value into it.
- Check if front==NULL, if it is true then front = rear = (newly created node)
- If it is false then rear=(newly created node) and rear node always contains the address of the front node.
- deQueue() This function is used to delete an element from the circular queue. In a queue, the element is always deleted from front position.
- Steps:
- Check whether queue is empty or not means front == NULL.
- If it is empty then display Queue is empty. If queue is not empty then step 3
- Check if (front==rear) if it is true then set front = rear = NULL else move the front forward in queue, update address of front in rear node and return the element.
Below is the implementation of above approach:
C++
// C++ program for insertion and// deletion in Circular Queue#include <bits/stdc++.h>usingnamespacestd;// Structure of a NodestructNode {intdata;structNode* link;};structQueue {structNode *front, *rear;};// Function to create Circular queuevoidenQueue(Queue* q,intvalue){structNode* temp =newNode;temp->data = value;if(q->front == NULL)q->front = temp;elseq->rear->link = temp;q->rear = temp;q->rear->link = q->front;}// Function to delete element from Circular QueueintdeQueue(Queue* q){if(q->front == NULL) {printf("Queue is empty");returnINT_MIN;}// If this is the last node to be deletedintvalue;// Value to be dequeuedif(q->front == q->rear) {value = q->front->data;free(q->front);q->front = NULL;q->rear = NULL;}else// There are more than one nodes{structNode* temp = q->front;value = temp->data;q->front = q->front->link;q->rear->link = q->front;free(temp);}returnvalue;}// Function displaying the elements of Circular QueuevoiddisplayQueue(structQueue* q){structNode* temp = q->front;printf("\nElements in Circular Queue are: ");while(temp->link != q->front) {printf("%d ", temp->data);temp = temp->link;}printf("%d", temp->data);}/* Driver of the program */intmain(){// Create a queue and initialize front and rearQueue* q =newQueue;q->front = q->rear = NULL;// Inserting elements in Circular QueueenQueue(q, 14);enQueue(q, 22);enQueue(q, 6);// Display elements present in Circular QueuedisplayQueue(q);// Deleting elements from Circular Queueprintf("\nDeleted value = %d", deQueue(q));printf("\nDeleted value = %d", deQueue(q));// Remaining elements in Circular QueuedisplayQueue(q);enQueue(q, 9);enQueue(q, 20);displayQueue(q);return0;}Java
// Java program for insertion and// deletion in Circular Queueimportjava.util.*;classSolution {// Structure of a NodestaticclassNode {intdata;Node link;}staticclassQueue {Node front, rear;}// Function to create Circular queuestaticvoidenQueue(Queue q,intvalue){Node temp =newNode();temp.data = value;if(q.front ==null)q.front = temp;elseq.rear.link = temp;q.rear = temp;q.rear.link = q.front;}// Function to delete element from Circular QueuestaticintdeQueue(Queue q){if(q.front ==null) {System.out.printf("Queue is empty");returnInteger.MIN_VALUE;}// If this is the last node to be deletedintvalue;// Value to be dequeuedif(q.front == q.rear) {value = q.front.data;q.front =null;q.rear =null;}else// There are more than one nodes{Node temp = q.front;value = temp.data;q.front = q.front.link;q.rear.link = q.front;}returnvalue;}// Function displaying the elements of Circular QueuestaticvoiddisplayQueue(Queue q){Node temp = q.front;System.out.printf("\nElements in Circular Queue are: ");while(temp.link != q.front) {System.out.printf("%d ", temp.data);temp = temp.link;}System.out.printf("%d", temp.data);}/* Driver of the program */publicstaticvoidmain(String args[]){// Create a queue and initialize front and rearQueue q =newQueue();q.front = q.rear =null;// Inserting elements in Circular QueueenQueue(q,14);enQueue(q,22);enQueue(q,6);// Display elements present in Circular QueuedisplayQueue(q);// Deleting elements from Circular QueueSystem.out.printf("\nDeleted value = %d", deQueue(q));System.out.printf("\nDeleted value = %d", deQueue(q));// Remaining elements in Circular QueuedisplayQueue(q);enQueue(q,9);enQueue(q,20);displayQueue(q);}}// This code is contributed// by Arnab KunduPython3
# Python3 program for insertion and# deletion in Circular Queue# Structure of a NodeclassNode:def__init__(self):self.data=Noneself.link=NoneclassQueue:def__init__(self):front=Nonerear=None# Function to create Circular queuedefenQueue(q, value):temp=Node()temp.data=valueif(q.front==None):q.front=tempelse:q.rear.link=tempq.rear=tempq.rear.link=q.front# Function to delete element from# Circular QueuedefdeQueue(q):if(q.front==None):print("Queue is empty")return-999999999999# If this is the last node to be deletedvalue=None# Value to be dequeuedif(q.front==q.rear):value=q.front.dataq.front=Noneq.rear=Noneelse:# There are more than one nodestemp=q.frontvalue=temp.dataq.front=q.front.linkq.rear.link=q.frontreturnvalue# Function displaying the elements# of Circular QueuedefdisplayQueue(q):temp=q.frontprint("Elements in Circular Queue are: ",end=" ")while(temp.link !=q.front):print(temp.data, end=" ")temp=temp.linkprint(temp.data)# Driver Codeif__name__=='__main__':# Create a queue and initialize# front and rearq=Queue()q.front=q.rear=None# Inserting elements in Circular QueueenQueue(q,14)enQueue(q,22)enQueue(q,6)# Display elements present in# Circular QueuedisplayQueue(q)# Deleting elements from Circular Queueprint("Deleted value = ", deQueue(q))print("Deleted value = ", deQueue(q))# Remaining elements in Circular QueuedisplayQueue(q)enQueue(q,9)enQueue(q,20)displayQueue(q)# This code is contributed by PranchalKC#
// C# program for insertion and// deletion in Circular QueueusingSystem;usingSystem.Collections.Generic;publicclassGFG {// Structure of a NodepublicclassNode {publicintdata;publicNode link;}publicclassLinkedList {publicNode front, rear;}// Function to create Circular queuepublicstaticvoidenQueue(LinkedList q,intvalue){Node temp =newNode();temp.data = value;if(q.front ==null) {q.front = temp;}else{q.rear.link = temp;}q.rear = temp;q.rear.link = q.front;}// Function to delete element from// Circular QueuepublicstaticintdeQueue(LinkedList q){if(q.front ==null) {Console.Write("Queue is empty");returnint.MinValue;}// If this is the last node to be deletedintvalue;// Value to be dequeuedif(q.front == q.rear) {value = q.front.data;q.front =null;q.rear =null;}else// There are more than one nodes{Node temp = q.front;value = temp.data;q.front = q.front.link;q.rear.link = q.front;}returnvalue;}// Function displaying the elements// of Circular QueuepublicstaticvoiddisplayQueue(LinkedList q){Node temp = q.front;Console.Write("\nElements in Circular Queue are: ");while(temp.link != q.front) {Console.Write("{0:D} ", temp.data);temp = temp.link;}Console.Write("{0:D}", temp.data);}// Driver CodepublicstaticvoidMain(string[] args){// Create a queue and initialize// front and rearLinkedList q =newLinkedList();q.front = q.rear =null;// Inserting elements in Circular QueueenQueue(q, 14);enQueue(q, 22);enQueue(q, 6);// Display elements present in// Circular QueuedisplayQueue(q);// Deleting elements from Circular QueueConsole.Write("\nDeleted value = {0:D}",deQueue(q));Console.Write("\nDeleted value = {0:D}",deQueue(q));// Remaining elements in Circular QueuedisplayQueue(q);enQueue(q, 9);enQueue(q, 20);displayQueue(q);}}// This code is contributed by Shrikant13
Output:Elements in Circular Queue are: 14 22 6 Deleted value = 14 Deleted value = 22 Elements in Circular Queue are: 6 Elements in Circular Queue are: 6 9 20
Time Complexity: Time complexity of enQueue(), deQueue() operation is O(1) as there is no loop in any of the operation.
Note: In case of linked list implementation, a queue can be easily implemented without being circular. However, in the case of array implementation, we need a circular queue to save space.
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




