Queue Code
Queue Code
class LinearQueue {
private:
public:
LinearQueue() {
front = -1;
rear = -1;
cout << "Initial state => Front: " << front << ", Rear: " << rear << endl;
bool isFull() {
bool isEmpty() {
if (isFull()) {
cout << " Queue Overflow! Cannot insert " << value << endl;
displayPointers();
return;
}
if (isEmpty()) {
front = 0;
rear++;
Q[rear] = value;
displayPointers();
void dequeue() {
if (isEmpty()) {
displayPointers();
return;
front++;
displayPointers();
}
void display() {
if (isEmpty()) {
return;
void displayPointers() {
cout << "Front: " << front << ", Rear: " << rear << endl;
};
int main() {
LinearQueue q;
do {
cout << "1. Insert (enqueue)\n2. Delete (dequeue)\n3. Display Queue\n4. Exit\n";
case 1:
q.enqueue(value);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
break;
default:
return 0;
}
#include <iostream>
class CircularQueue {
private:
public:
CircularQueue() {
front = -1;
rear = -1;
cout << "Initial state => Front: " << front << ", Rear: " << rear << endl;
bool isFull() {
bool isEmpty() {
if (isFull()) {
cout << " Queue Overflow! Cannot insert " << value << endl;
displayPointers();
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear++;
Q[rear] = value;
displayPointers();
void dequeue() {
if (isEmpty()) {
displayPointers();
return;
if (front == rear) {
} else {
front++;
displayPointers();
void display() {
if (isEmpty()) {
return;
} else {
void displayPointers() {
cout << "Front: " << front << ", Rear: " << rear << endl;
};
int main() {
CircularQueue q;
do {
cout << "1. Insert (enqueue)\n2. Delete (dequeue)\n3. Display Queue\n4. Exit\n";
switch (choice) {
case 1:
q.enqueue(value);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
break;
default:
return 0;
}
#include <iostream>
class Deque {
private:
int Q[5];
public:
Deque() {
front = -1;
rear = -1;
cout << "Initial state => Front: " << front << ", Rear: " << rear << endl;
bool isFull() {
bool isEmpty() {
if (isFull()) {
displayPointers();
return;
}
if (isEmpty()) {
front = rear = 0;
} else if (front == 0) {
front = SIZE - 1;
} else {
front--;
Q[front] = value;
displayPointers();
if (isFull()) {
displayPointers();
return;
if (isEmpty()) {
front = rear = 0;
rear = 0;
} else {
rear++;
}
Q[rear] = value;
cout << " Inserted at Rear: " << value << endl;
displayPointers();
void deleteFront() {
if (isEmpty()) {
displayPointers();
return;
if (front == rear) {
front = 0;
} else {
front++;
cout << "Deleted from Front: " << deletedValue << endl;
displayPointers();
void deleteRear() {
if (isEmpty()) {
cout << " Deque Underflow at Rear!\n";
displayPointers();
return;
if (front == rear) {
} else if (rear == 0) {
rear = SIZE - 1;
} else {
rear--;
cout << " Deleted from Rear: " << deletedValue << endl;
displayPointers();
void display() {
if (isEmpty()) {
return;
int i = front;
while (true) {
cout << Q[i] << " ";
if (i == rear) break;
i = (i + 1) % SIZE;
void displayPointers() {
cout << "Front: " << front << ", Rear: " << rear << endl;
};
int main() {
Deque dq;
do {
cout << "1. Insert at Front\n2. Insert at Rear\n3. Delete from Front\n4. Delete from Rear\n";
switch (choice) {
case 1:
dq.insertFront(value);
break;
case 2:
dq.insertRear(value);
break;
case 3:
dq.deleteFront();
break;
case 4:
dq.deleteRear();
break;
case 5:
dq.display();
break;
case 6:
break;
default:
return 0;
}
What Is Modulo (%)?
The modulo operator gives the remainder after division. For example:
In a normal queue (linear), when you reach the last index of the array, you can't go further. But in
a circular queue or deque, when you hit the last index, you "wrap around" to the beginning —
just like going in a circle.
Suppose we have:
When the rear reaches index 4 and we want to insert more (if space is available), we loop back to
index 0 using:
Although in array form they appear disjointed, logically the queue is:
30 → 40 → 50 → 60 → 70
int i = front;
while (true) {
cout << Q[i] << " ";
if (i == rear) break;
i = (i + 1) % SIZE;
}
Start at i = 2 → Q[2] = 30
Next: (2 + 1) % 5 = 3 → Q[3] = 40
Next: (3 + 1) % 5 = 4 → Q[4] = 50
Next: (4 + 1) % 5 = 0 → Q[0] = 60
Next: (0 + 1) % 5 = 1 → Q[1] = 70
Now i == rear, so break the loop
This way, the queue prints in logical order even though physically the elements are stored across
the end and start of the array.
So, (i + 1) % SIZE is the key trick that powers circular queues and deques!