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
#define MAX 5
char queue[MAX];
int front = -1, rear = -1;
// a. Insert into Circular Queue
void insert(char ch) {
if ((front == 0 && rear == MAX - 1) || (rear + 1 == front)) {
printf("Queue Overflow!\n");
return;
}
if (front == -1) front = rear = 0;
else rear = (rear + 1) % MAX;
queue[rear] = ch;
}
// b. Delete from Circular Queue
void delete() {
if (front == -1) {
printf("Queue Underflow!\n");
return;
}
printf("Deleted: %c\n", queue[front]);
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX;
}
// c/d. Display Circular Queue
void display() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Queue: ");
int i = front;
while (1) {
printf("%c ", queue[i]);
if (i == rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}
int main() {
int choice;
char ch;
do {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter character to insert: ");
scanf(" %c", &ch);
insert(ch);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 4);
return 0;
}
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter character to insert: A
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter character to insert: B
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter character to insert: C
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter character to insert: D
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter character to insert: E
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter character to insert: F
Queue Overflow!
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 3
Queue: A B C D E
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Deleted: A
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 3
Queue: B C D E
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 4
Exiting...
Replay !
Share Your Thoughts