#include <iostream>
using namespace std;
int main()
{
int q[5], front = -1, rear = -1;
int choice, value, i;
do
{
cout << "\n1. Insert";
cout << "\n2. Delete";
cout << "\n3. Display";
cout << "\n4. Exit";
cout << "\nEnter choice: ";
cin >> choice;
switch (choice)
{
case 1:
if ((rear + 1) % 5 == front)
cout << "Queue is Full";
For example, if rear = 4 and front = 0:
[ (4+1)%5=0 ]
Since 0 == front, the queue is full.
else
{
cout << "Enter value: ";
cin >> value;
if (front == -1)
front = 0;
If the queue is empty, front is set to 0 because the first element is being inserted.
rear = (rear + 1) % 5;
q[rear] = value;
}
rear = (rear + 1) % 5; moves rear to the next position.
q[rear] = value; stores the value at that position.
Example: If rear = -1, then:[ (-1+1)%5=0 ]
Therefore, rear = 0, and the value is stored at q[0].
break;
case 2:
if (front == -1)
cout << "Queue is Empty";
else
{
cout << "Deleted: " << q[front];
if (front == rear)
front = rear = -1;
If front == rear, there is only one element in the queue.
After deleting it, both front and rear are set to -1, indicating that the queue is empty.
else
front = (front + 1) % 5;
This moves front to the next position.
For example, if front = 2:
[ (2+1)%5=3 ]
Now, front = 3.
}
break;
case 3:
if (front == -1)
cout << "Queue is Empty";
else
{
i = front;
The variable i starts at the front position because we want to display elements from the beginning of the queue.
while (true)
{
cout << q[i] << " ";
if (i == rear)
When i reaches rear, the last element has been displayed. break stops the loop.
break;
i = (i + 1) % 5;
}
}
break;
}
} while (choice != 4);
return 0;
}




