How to implement a circular queue in C++
A circular queue is a linear data structure in which operations are performed based on the FIFO (First In First Out) principle; the last position is connected back to the first position to make a circle.
In a queue, we can insert elements until the queue becomes full; but once it is full, we cannot insert an element even if there is a space in front of it.
Once your entire circular queue is filled, if you delete some data, you’ll be able to put in more data as the rear variable will start repeating itself. In other words, once the value of your rear variable is n - 1 (the upper bound) and you delete some data from the front, more data will be able to be added into that vacated space.
Implementation
The simplest way to implement a circular queue is by using arrays. A circular queue has 2 key methods:
- enqueue()
- dequeue()
enqueue()
-
Check whether the queue is full. A queue is full when the
frontis next to therear. For example, with a queue of size 6, iffrontis 0 andrearis 5, or iffrontis 2 andrearis 1, it means that the queue is full. -
If it is full, then display
"Queue is full"; if the queue is not full, then check ifrearis the last index. If it is, setrearto 0; if it is not, incrementrearand add the value at that index.
dequeue()
-
Check whether the queue is empty (i.e., if
front/rearhas a value of -1). -
If it is empty, then it will display
"Queue is empty". If the queue is not empty, then check if the queue has only one value (i.e.,front==rear). If it does have only one value, set bothrearandfrontto-1; if it does not, check iffrontis the last index of the queue and, if so, setfrontto0; otherwise, incrementfront.
Code
#include<bits/stdc++.h>using namespace std;struct Queue{// Initialize front and rearint rear, front;// Circular Queueint 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))){printf("\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 Queueint Queue::deQueue(){if (front == -1){printf("\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;elsefront++;return data;}void Queue::displayQueue(){if (front == -1){printf("\nQueue is Empty");return;}printf("\nElements in Circular Queue are: ");if (rear >= front){for (int i = front; i <= rear; i++)printf("%d ",arr[i]);}else{for (int i = front; i < size; i++)printf("%d ", arr[i]);for (int i = 0; i <= rear; i++)printf("%d ", arr[i]);}}/* Driver of the program */int main(){Queue q(5);// Inserting elements in Circular Queueq.enQueue(10);q.enQueue(8);q.enQueue(-6);q.enQueue(16);// Display elements present in Circular Queueq.displayQueue();// Deleting elements from Circular Queueprintf("\nDeleted value = %d", q.deQueue());printf("\nDeleted value = %d", q.deQueue());q.displayQueue();q.enQueue(12);q.enQueue(20);q.enQueue(5);q.displayQueue();q.enQueue(20);return 0;}
Free Resources