Design, Develop and Implement a menu driven Program in C for the following operations on Circular
QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 3
int q[SIZE], f=0,r = -1, count = 0;
void insert_cq()
{
int item;
if (count == SIZE)
{
printf(" the queue overflow\n"); return;
}
printf("Enter the item for insertion\n");
scanf("%d",&item);r = (r + 1)%SIZE;
q[r] = item;
count++;
}
void delete_cq()
{
if (count == 0)
{
printf("Queue underflow\n"); return;
}
printf("Element deleted is %d ",q[f]);
f = (f + 1) % SIZE;
count--;
}
void display_cq()
{
int i,j =f;
if (count == 0)
{
printf("Queue is empty\n");
return;
}
printf(" The contents of queue are");
for ( i = 1; I <= count; i++)
{
printf("%d ",q[j]);
j = ( j + 1)%SIZE;
}
}
void main()
{
int ch;
for(;;)
{
printf("\[Link] [Link] [Link] 4: exit\n");
printf("Enter your choice\n");scanf("%d",&ch);
switch(ch)
{
case 1: insert_cq(); break;
case 2: delete_cq(); break;
case 3: display_cq(); break;
default :printf("invalid choice\n");
exit(0);
}
}
}
Output:-
[Link] [Link] [Link] 4: exit
Enter your choice
1
Enter the item for insertion
11
[Link] [Link] [Link] 4: exit
Enter your choice
1
Enter the item for insertion
12
[Link] [Link] [Link] 4: exit
Enter your choice
1
Enter the item for insertion
13
[Link] [Link] [Link] 4: exit
Enter your choice
1
the queue overflow
[Link] [Link] [Link] 4: exit
Enter your choice
3
The contents of queue are11 12 13
[Link] [Link] [Link] 4: exit
Enter your choice
2
Element deleted is 11
[Link] [Link] [Link] 4: exit
Enter your choice
3
The contents of queue are12 13
[Link] [Link] [Link] 4: exit
Enter your choice
1
Enter the item for insertion
14
[Link] [Link] [Link] 4: exit
Enter your choice
3
The contents of queue are12 13 14
[Link] [Link] [Link] 4: exit
Enter your choice
2
Element deleted is 12
[Link] [Link] [Link] 4: exit
Enter your choice
2
Element deleted is 13
[Link] [Link] [Link] 4: exit
Enter your choice
2
Element deleted is 14
[Link] [Link] [Link] 4: exit
Enter your choice
2
Queue underflow
[Link] [Link] [Link] 4: exit
Enter your choice
3
Queue is empty
[Link] [Link] [Link] 4: exit
Enter your choice