Write a program to perform the following operations on the Circular QUEUE: Insert,
Delete and Display.
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Define the maximum size of the circular queue
int queue[MAX];
int front = -1, rear = -1;
// Function to check if the circular queue is full
int isFull() {
return (front == (rear + 1) % MAX);
}
// Function to check if the circular queue is empty
int isEmpty() {
return (front == -1);
}
// Function to insert an element into the circular queue
void insert(int element) {
if (isFull()) {
printf("Queue Overflow! Cannot insert the element.\n");
} else {
if (front == -1) // When inserting the first element
front = 0;
rear = (rear + 1) % MAX;
queue[rear] = element;
printf("Element %d inserted into the queue.\n", element);
}
}
// Function to delete an element from the circular queue
void delete() {
if (isEmpty()) {
printf("Queue Underflow! Cannot delete any element.\n");
} else {
printf("Element %d deleted from the queue.\n", queue[front]);
if (front == rear) { // Queue is now empty after deleting the last element
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}
// Function to display the elements of the circular queue
void display() {
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
}
int main() {
int choice, element;
while (1) {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf("%d", &element);
insert(element);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}