Circular Queue with (Add, Delete, Show Options) in C++.

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 20
char queue[MAX];
int front, rear;

void add_Q(char queue[], int front, char val, int &rear);
char del_Q(char queue[], int &front, int rear);
void show_Q(char queue[], int front, int rear);

void main()
{
int ch;
char val;
char op = 'Y';
rear = -1;
front = -1;
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t\t======================";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter youc choice: ";
cin >> ch;
switch (ch)
{
case 1:
do
{
cout << "Enter the value to be added in queue ";
cin >> val;
add_Q(queue, front, val, rear);
cout << "Do you want to add more <Y/N>? ";
cin >> op;
} while (op == 'Y' || op == 'y');
break;
case 2:
op = 'Y'; // Initialize for the second loop
do
{
val = del_Q(queue, front, rear);
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more <Y/N>? ";
cin >> op;
} while (op == 'Y' || op == 'y');
break;
case 3:
show_Q(queue, front, rear);
break;
case 4:
exit(0);
}
}
while (ch != 4);
}

void add_Q(char queue[], int front, char val, int &rear)
{
if ((rear + 1) %  MAX == front)
{
cout << "Queue Full ";
}
else
{
rear = (rear + 1) % MAX;
queue[rear] = val;
}
}

char del_Q(char queue[], int &front, int rear)
{
char value;
if (front == rear)
{
cout << "Queue Empty ";
value = -1;
}
else
{
front = (front + 1) % MAX;
value = queue[front];
}
return (value);
}

void show_Q(char queue[], int front, int rear)
{
clrscr();
cout << "The values are ";
do
{
front = (front + 1) % MAX;
cout << "\n" << queue[front];
}while(front != rear);
}








No comments

Post your comments

Powered by Blogger.