Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Queue
  • Practice Queue
  • MCQs on Queue
  • Queue Tutorial
  • Operations
  • Applications
  • Implementation
  • Stack vs Queue
  • Types of Queue
  • Circular Queue
  • Deque
  • Priority Queue
  • Stack using Queue
  • Advantages & Disadvantages
Open In App
Next Article:
Circular Linked List Implementation of Circular Queue
Next article icon

Circular Linked List Implementation of Circular Queue

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 

Circular-Linked-List-Implementation-of-Circular-Queue


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:

  1. Front(): return the front node's value if not null. Otherwise return -1.
  2. Rear(): return the rear node's value if not null. Otherwise return -1.
  3. 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.
  4. 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); 

Output
Front 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.


Next Article
Circular Linked List Implementation of Circular Queue

A

Akash Gupta
Improve
Article Tags :
  • Linked List
  • Queue
  • DSA
  • circular linked list
  • circular-array
Practice Tags :
  • circular linked list
  • Linked List
  • 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 inser
    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 to
    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
    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. Circular doubly linked listCharacteristics of Circular Doubly Linked List :A circular doubly linked list has the following properties: Circular: A
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences