#include <stdio.
h>
#include <stdlib.h>
#define SIZE 5 // Define the size of the Circular Queue
// Global variables for the Circular Queue
int queue[SIZE];
int front = -1, rear = -1;
// Function to check if the queue is empty
int isEmpty() {
return (front == -1);
}
// Function to check if the queue is full
int isFull() {
return ((rear + 1) % SIZE == front);
}
// Function to add an element to the queue
void enqueue(int value) {
if (isFull()) {
printf("Queue is Full! Cannot enqueue %d\n", value);
return;
}
if (isEmpty()) {
front = 0; // Initialize front if the queue is empty
}
rear = (rear + 1) % SIZE; // Move rear to next position in circular manner
queue[rear] = value;
printf("%d enqueued to the queue.\n", value);
}
// Function to remove an element from the queue
void dequeue() {
if (isEmpty()) {
printf("Queue is Empty! Cannot dequeue.\n");
return;
}
printf("%d dequeued from the queue.\n", queue[front]);
if (front == rear) { // Queue has only one element
front = rear = -1; // Reset the queue
} else {
front = (front + 1) % SIZE; // Move front to next position in circular
manner
}
}
// Function to display the queue
void display() {
if (isEmpty()) {
printf("Queue is Empty!\n");
return;
}
printf("Queue elements: ");
for (int i = front; i!=rear+1; i=(i+1)%SIZE)
{
printf("%d",queue[i]);
}
/*while (1) {
int i = front;
printf("%d ", queue[i]);
if (i == rear) break; // Exit when all elements are printed
i = (i + 1) % SIZE; // Move to next element in circular manner
}*/
printf("\n");
}
// Main function for menu-driven program
int main() {
int choice, value;
while (1) {
printf("\n--- Circular Queue Menu ---\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
gets("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
gets("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}