Program 6:
Develop 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
// Online C compiler to run C program online
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
char cq[MAX];
int front = -1, rear = -1;
void insert(char item);
void delete();
void display();
int main() {
int ch;
char item;
while(1) {
printf("\n\nMain Menu");
printf("\n1. Insertion");
printf("\n2. Deletion");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("\nEnter the element to be inserted: ");
scanf(" %c", &item); // Notice the space before %c
insert(item);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nPlease enter a valid choice");
}
}
return 0;
}
void insert(char item) {
if ( (front == 0 && rear == MAX - 1) ||
(rear + 1) % MAX == front ) {
printf("\nCircular Queue Overflow");
return;
}
if (front == -1) { // first element
front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX;
}
cq[rear] = item;
printf("\nInserted: %c", item);
}
void delete() {
if (front == -1) {
printf("\nCircular Queue Underflow");
return;
}
char item = cq[front];
printf("\nDeleted element: %c", item);
if (front == rear) { // only one element
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
void display() {
if (front == -1) {
printf("\nCircular Queue Empty");
return;
}
printf("\nCircular Queue elements:\n");
int i = front;
while (1) {
printf(" %c", cq[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
}