ASSIGNMENT 3 – CIRCULAR QUEUE
NAME – YASH SANJAY SURYAWANSHI
ROLL NO – 63
SE IT – BATCH 4
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1;
void insert(int x)
{
if (rear == -1) // empty
{
rear = 0;
front = 0;
cqueue[rear] = x;
cout << "\nInserted";
}
else // not empty
{
rear = (rear + 1) % 10;
cqueue[rear] = x;
cout << "\nInserted";
}
}
void display()
{
for (int i = front; i != rear; i = (i + 1) % 5)
{
cout << cqueue[i] << " ";
}
cout << cqueue[rear];
}
int deletee()
{
int x = cqueue[front];
if (rear == front)
{
rear = front = -1;
}
else
{
front = (front + 1) % 5;
}
return x;
}
int main()
{
int ch;
do
{
cout << "\nMENU
\[Link]\[Link]\[Link]\[Link]";
cout << "\nEnter your choice: ";
cin >> ch;
cout << "--------";
switch (ch)
{
case 1:
int x, n;
if ((rear + 1) % 5 == front) // full
{
cout << "\nQueue is full!!";
exit(0);
}
cout << "\nEnter element to be inserted:
";
cin >> x;
insert(x);
break;
case 2:
cout << "Elements of circular queue: ";
display();
break;
case 3:
if (rear == -1)
{
cout << "\nQueue is empty";
exit(0);
}
x = deletee();
cout << "\nDeleted element: " << x;
break;
}
} while (ch != 4);
}
OUTPUT:
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
--------
Enter element to be inserted: 1
Inserted
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
--------
Enter element to be inserted: 2
Inserted
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
--------
Enter element to be inserted: 3
Inserted
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice: 2
--------Elements of circular queue: 1 2 3
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice: 3
--------
Deleted element: 1
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice: 2
--------Elements of circular queue: 2 3