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
#include <stdio.h>
#define MAX 5
char queue[MAX];
int front = -1, rear = -1;
// Insert an element
void insert() {
char ch;
if ((front == 0 && rear == MAX - 1) || (front == rear + 1))
{
printf("\nQueue Overflow! Cannot insert.\n");
return;
}
printf("Enter a character to insert: ");
scanf(" %c", &ch);
if (front == -1) // first element
front = rear = 0;
else if (rear == MAX - 1)
rear = 0;
else
rear++;
queue[rear] = ch;
printf("Inserted '%c' into the queue.\n", ch);
}
// Delete an element
void delete() {
if (front == -1) {
printf("\nQueue Underflow! Nothing to delete.\n");
return;
}
printf("Deleted element: %c\n", queue[front]);
if (front == rear) // only one element
front = rear = -1;
else if (front == MAX - 1)
front = 0;
else
front++;
}
// Display the queue
void display() {
int i;
if (front == -1) {
printf("\nQueue is empty.\n");
return;
}
printf("\nQueue elements are: ");
if (front <= rear) {
for (i = front; i <= rear; i++)
printf("%c ", queue[i]);
} else {
for (i = front; i < MAX; i++)
printf("%c ", queue[i]);
for (i = 0; i <= rear; i++)
printf("%c ", queue[i]);
}
printf("\n");
}
// Main menu
int main() {
int choice;
printf(" Circular Queue Operations (Characters) \n");
while (1) {
printf("\nMenu:\n");
printf("1. Insert an Element\n");
printf("2. Delete an Element\n");
printf("3. Display Queue\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insert(); break;
case 2: delete(); break;
case 3: display(); break;
default: printf("Invalid choice! Try again.\n");
}
}
return 0;
}
Sample Output
=== Circular Queue Operations (Characters) ===
Menu:
1. Insert an Element
2. Delete an Element
3. Display Queue
Enter your choice: 1
Enter a character to insert: A
Inserted 'A' into the queue.
Enter your choice: 1
Enter a character to insert: B
Inserted 'B' into the queue.
Enter your choice: 3
Queue elements are: A B
Enter your choice: 2
Deleted element: A
Enter your choice: 3
Queue elements are: B