Circular Queue in Data Structure
Data Structure : Circular Queue
Circular Queue in data structure is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position.
A circular queue overcomes the problem of unused space in normal queues, and it can be implemented on both arrays and linked list.
Why Circular Queues are needed ?
When we talk about linear queues, once the queue becomes full, we can not insert more elements. Even though we dequeue a few of the elements, only once all the elements are dequeued then only queue is reset and then new elements can be inserted. The example of the same is shown in image below –
How Circular Queues work in Data Structure
Circular queues work very similarly as linear queues with minor addition and enhancements. Any circular queue as the following –
- Front – The starting head of the circular queue
- Rear – The ending tail of the circular queue
- Enqueue Operation – Process of adding a new item in the queue
- Dequeue Operation – Process of removing an existing item from the queue
- Overflow Condition – When the queue is full
- Underflow Condition – When queue is empty
- Size – The max number of items the circular queue can hold
#include<stdio.h> #define SIZE 6 int array[SIZE]; int front = -1, rear = -1; // Function to check if the queue is full int isFull(){ if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)){ return 1; } return 0; } // Function to check if the queue is empty int isEmpty(){ if (front == -1) { return 1; } return 0; } // Enqueueing in Queue void enqueue(int value){ if (isFull()) printf("Can't add the queue is full \n"); else { if (front == -1) front = 0; rear = (rear + 1) % SIZE; array[rear] = value; printf("%d was added\n", value); } } // Dequeueing in Queue int dequeue() { int element; if (isEmpty()) { printf("Can't remove the queue is empty \n"); return (-1); } else { element = array[front]; //The has only 1 item so we need to put rear and front values to - 1 //After dequeue queue is empty if (front == rear) { front = -1; rear = -1; } else { front = (front + 1) % SIZE; } printf("%d dequeued\n", element); return (1); } } // Display the queue void print(){ int i; if (isEmpty()) printf("Empty Queue\n"); else { printf("\nThe queue looks like: \n"); for (i = front; i != rear; i = (i + 1) % SIZE) { printf("%d ", array[i]); } printf("%d \n\n", array[i]); } } int main() { // Can't as no elements in the queue both front & rear at -1 dequeue(); enqueue(10); enqueue(20); enqueue(30); enqueue(40); enqueue(50); print(); dequeue(); dequeue(); print(); enqueue(60); enqueue(70); enqueue(80); enqueue(90);//can't add the queue is full print(); return 0; }
//Circular queue C++ program #include<bits/stdc++.h> using namespace std; struct Queue { // Initialize front and rear int rear, front; // Circular Queue int size; int *arr; Queue (int s){ front = rear = -1; size = s; arr = new int[s]; } void enQueue (int value); int deQueue (); void displayQueue (); }; /* Function to create Circular queue */ void Queue::enQueue (int value) { if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) { cout << "\nQueue is Full"; return; } else if (front == -1) /* Insert First Element */ { front = rear = 0; arr[rear] = value; } else if (rear == size - 1 && front != 0) { rear = 0; arr[rear] = value; } else{ rear++; arr[rear] = value; } } // Function to delete element from Circular Queue int Queue::deQueue () { if (front == -1) { cout << "\nQueue is Empty"; return INT_MIN; } int data = arr[front]; arr[front] = -1; if (front == rear) { front = -1; rear = -1; } else if (front == size - 1) front = 0; else front++; return data; } // Function displaying the elements // of Circular Queue void Queue::displayQueue () { if (front == -1) { cout << "\nQueue is Empty"; return; } cout << "\nElements in Circular Queue are: "; if (rear >= front) { for (int i = front; i <= rear; i++) cout<<arr[i]<<" "; } else { for (int i = front; i < size; i++) cout<<arr[i]<<" "; for (int i = 0; i <= rear; i++) cout<<arr[i]<<" "; } } int main () { Queue q (5); // Inserting elements in Circular Queue q.enQueue (10); q.enQueue (20); q.enQueue (30); q.enQueue (40); // Display elements present in Circular Queue q.displayQueue (); // Deleting elements from Circular Queue cout << "\nDeleted value = " << q.deQueue (); cout << "\nDeleted value = " << q.deQueue (); q.displayQueue (); q.enQueue (50); q.enQueue (60); q.enQueue (70); q.displayQueue (); return 0; }
import java.util.*; class CircularQueue { private int size, front, rear; //Variable declaration private ArrayList < Integer > queue = new ArrayList < Integer > (); //Declaring Integer array list CircularQueue (int size) //Constructor { this.size = size; this.front = this.rear = -1; } public void enQueue (int value) //Insertion Function { if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) // Condition if queue is full { System.out.print ("Queue Full!"); } else if (front == -1) // Condition for empty queue. { front = 0; rear = 0; queue.add (rear, value); } else if (rear == size - 1 && front != 0) { rear = 0; queue.set (rear, value); } else { rear = (rear + 1); // Adding a new element if if (front <= rear) { queue.add (rear, value); } // Else updating old value else { queue.set (rear, value); } } } public int deQueue () //Dequeue Function { int temp; if (front == -1) //Checking for empty queue { System.out.print ("Queue Empty!"); return -1; } temp = queue.get (front); if (front == rear) // For only one element { front = -1; rear = -1; } else if (front == size - 1) { front = 0; } else { front = front + 1; } return temp; // Returns dequeued element } public void displayQueue () // Display the elements of queue { if (front == -1) // Check for empty queue { System.out.print ("Queue is Empty"); return; } System.out.print ("Elements in the " + "circular queue are: "); if (rear >= front) //if rear has not crossed the size limit { for (int i = front; i <= rear; i++) //print elements using loop { System.out.print (queue.get (i)); System.out.print (" "); } System.out.println (); } else { for (int i = front; i < size; i++) { System.out.print (queue.get (i)); System.out.print (" "); } for (int i = 0; i <= rear; i++) // Loop for printing elements from 0th index till rear position { System.out.print (queue.get (i)); System.out.print (" "); } System.out.println (); } } } public class Main { public static void main (String[]args) { CircularQueue queue = new CircularQueue (5); // Initialising new object of CircularQueue class. queue.enQueue (1); queue.enQueue (2); queue.enQueue (3); queue.enQueue (4); queue.enQueue (5); queue.displayQueue (); int x = queue.deQueue (); if (x != -1) // Check for empty queue { System.out.print ("Deleted value = "); System.out.println (x); } x = queue.deQueue (); if (x != -1) // Check for empty queue { System.out.print ("Deleted value = "); System.out.println (x); } queue.displayQueue (); queue.enQueue (6); queue.enQueue (7); queue.displayQueue (); queue.enQueue (8); } }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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