C Program to Implement Circular Queue Last Updated : 02 Jun, 2024 Comments Improve Suggest changes Like Article Like Report A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the last position back to the first, forming a circle. In this article, we will learn how to implement circular queue in C programming language. What is a Circular Queue in C?In a circular queue, the last element is connected to the first element. This structure allows efficient use of space, as it prevents the need for data shifting when the queue is full but has vacant spaces at the front. Why Use a Circular Queue in C?The circular queue can be implemented using both an array or a circular linked list but it is most beneficial for array implementation as it resolves the stack overflow condition of the linear implementation even when the whole array is empty. So, here we will only discuss the array implementation of the circular queue Basic Operations in C Circular QueueOperation Description Time Complexity Space Complexity Enqueue Adds an element to the rear of the queue. Checks if the queue is full before adding. O(1) O(1) Dequeue Removes an element from the front of the queue. Checks if the queue is empty before removing. O(1) O(1) Peek Returns the front element without removing it. O(1) O(1) IsFull Checks if the queue is full. O(1) O(1) IsEmpty Checks if the queue is empty.O(1) O(1)Implementation of the Basic Circular Queue OperationsIsFull ImplementationCheck if the front index is equal to the rear index.If true, the queue is empty; return true.Otherwise, return false.IsEmpty ImplementationCheck if the (rear + 1) % MAX_SIZE is equal to the front index.If true, the queue is full; return true.Otherwise, return false.Enqueue ImplementationCheck if the queue is full using the isFull function.If the queue is full, print "Queue Overflow" and return.Otherwise, increment the rear index using (rear + 1) % MAX_SIZE.Insert the new element at the rear index.Dequeue ImplementationCheck if the queue is empty using the isEmpty function.If the queue is empty, print "Queue Underflow" and return.Otherwise, increment the front index using (front + 1) % MAX_SIZE.Peek ImplementationCheck if the queue is empty using the isEmpty function.If the queue is empty, print "Queue is Empty" and return -1.Otherwise, return the element at (front + 1) % MAX_SIZE.Program for Circular Queue Implementation in C C // C Program to implement the circular queue in c using arrays #include <stdio.h> // Define the maximum size of the queue #define MAX_SIZE 5 // Declare the queue array and front, rear variables int queue[MAX_SIZE]; int front = -1, rear = -1; // Function to check if the queue is full int isFull() { // If the next position is the front, the queue is full return (rear + 1) % MAX_SIZE == front; } // Function to check if the queue is empty int isEmpty() { // If the front hasn't been set, the queue is empty return front == -1; } // Function to enqueue (insert) an element void enqueue(int data) { // If the queue is full, print an error message and // return if (isFull()) { printf("Queue overflow\n"); return; } // If the queue is empty, set the front to the first // position if (front == -1) { front = 0; } // Add the data to the queue and move the rear pointer rear = (rear + 1) % MAX_SIZE; queue[rear] = data; printf("Element %d inserted\n", data); } // Function to dequeue (remove) an element int dequeue() { // If the queue is empty, print an error message and // return -1 if (isEmpty()) { printf("Queue underflow\n"); return -1; } // Get the data from the front of the queue int data = queue[front]; // If the front and rear pointers are at the same // position, reset them if (front == rear) { front = rear = -1; } else { // Otherwise, move the front pointer to the next // position front = (front + 1) % MAX_SIZE; } // Return the dequeued data return data; } // Function to display the queue elements void display() { // If the queue is empty, print a message and return if (isEmpty()) { printf("Queue is empty\n"); return; } // Print the elements in the queue printf("Queue elements: "); int i = front; while (i != rear) { printf("%d ", queue[i]); i = (i + 1) % MAX_SIZE; } // Print the last element printf("%d\n", queue[rear]); } // Main function int main() { // Enqueue some elements enqueue(10); enqueue(20); enqueue(30); // Display the queue display(); // Dequeue an element and print it printf("Dequeued element: %d\n", dequeue()); // Display the queue again display(); // End of main function return 0; } ``` OutputElement 10 inserted Element 20 inserted Element 30 inserted Queue elements: 10 20 30 Dequeued element: 10 Queue elements: 20 30 For linked list implementation of circular queue, refer to this article - Circular Queue Using Linked List Comment More infoAdvertise with us Next Article C Program to Implement Circular Queue H heysaiyad Follow Improve Article Tags : C Language C-DSA Similar Reads C++ Program to Implement Circular Queue In C++, Queues are a fundamental data structure in computer science which works on the principle of FIFO (First In, First Out). They can be implemented using both array and linked list. A circular queue is a type of queue in which the last element is connected to the first element, forming a circula 7 min read C Program to Implement Circular Linked List A linked list is a dynamic data structure where elements are not stored in contiguous memory locations but linked using pointers. In a circular linked list, the last node points back to the first node instead of pointing to NULL, forming a circle.In this article, we will learn how to implement the c 8 min read C Program to Implement Priority Queue Priority queue is an abstract data type(ADT) in the computer science which is designed to the operate much like the regular queue except that each element has the certain priority. The priority can determines the order in which elements are dequeued - elements with the higher priority are removed fr 6 min read C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu 8 min read JavaScript program to Implement a Circular Queue using Arrays A circular queue is a roundabout. When the last person in line is served, they go back to the start of the line. This way, there's no wasted space and the line keeps moving efficiently. It is an extended version of a conventional queue, a circular queue is created by joining the last element to the 3 min read C++ Program to Implement Queue using Linked List Queue is the fundamental data structure that follows the First In, First Out (FIFO) principle where the elements are added at the one end, called the rear and removed from other end called the front. In this article, we will learn how to implement queue in C++ using a linked list. Queue Using Linked 5 min read How to manage Full Circular Queue event? What is a circular Queue?A circular queue is a non-primitive, linear data structure. It is an extended version of a linear queue. It is also called a Circular buffer or cyclic buffer. It offers a quick and clean way of storing First First Out (FIFO) data with a maximum size. Mainly it is used in mem 12 min read Circular Queue in Python A Circular Queue is a kind of queue that can insert elements inside it dynamically. Suppose, in a given array there is not any space left at the rear but we have space at the front in the normal queue it is not possible to insert elements at the front but in the case of a circular queue we can do th 3 min read Perl | Implementing a Queue Prerequisite: StackQueue in Perl is a linear abstract data structure which follows the FIFO (First In First Out) order. It resembles the properties of the queue which we encounter in our daily life where the person who comes first will be served first. It is open at both ends. Unlike stack where the 10 min read Java Program to Implement the Queue Data Structure Queue is the fundamental data structure that follows the First-In-First-Out(FIFO) principle where the element that is inserted first is one that gets removed first. Imagine the queue of the people waiting in the line at the ticket counter: the person who arrives the first gets served first and so on 4 min read Like