C++ Circular Queue Data Structure
C++ Circular Queue Data Structure
In this tutorial, you will learn what a circular queue is. Also, you will find
implementation of circular queue in C, C++, Java and Python.
A circular queue is the extended version of a regular queue where the last
element is connected to the first element. Thus forming a circle-like
structure.
Here, the circular increment is performed by modulo division with the queue
size. That is,
2. Dequeue Operation
The second case happens when REAR starts from 0 due to circular increment
and when its value is just 1 less than FRONT , the queue is full.
#include <iostream>
#define SIZE 5 /* Size of Circular Queue */
class Queue {
private:
int items[SIZE], front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
// Check if the queue is full
bool isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
if (front == rear + 1) {
return true;
}
return false;
}
// Check if the queue is empty
bool isEmpty() {
if (front == -1)
return true;
else
return false;
}
// Adding an element
void enQueue(int element) {
void display() {
// Function to display status of Circular Queue
int i;
if (isEmpty()) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout << "Front -> " << front;
cout << endl
<< "Items -> ";
for (i = front; i != rear; i = (i + 1) % SIZE)
cout << items[i];
cout << items[i];
int main() {
Queue q;
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);
q.display();
if (elem != -1)
cout << endl
<< "Deleted Element is " << elem;
q.display();
q.enQueue(7);
q.display();
return 0;
}
• Memory management
• Traffic Management