#include <stdio.
h>
int cqueue[5];
int n, choice, item;
int rear = -1;
int front = -1;
void insert(int item);
int del();
int peek();
void display();
int main() {
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("\n1. Insert\n2. Delete\n3. Peek\n4. Display\n5. Exit\n");
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
printf("Enter the element to be inserted: ");
scanf("%d", &item);
insert(item);
break;
}
case 2: {
item = del();
if (item != -1) {
printf("The deleted element = %d\n", item);
}
break;
}
case 3: {
int peekedItem = peek();
if (peekedItem != -1) {
printf("The element at the peek of the circular queue = %d\n", peekedItem);
}
break;
}
case 4: {
display();
break;
}
case 5: {
printf("Exit\n");
break;
}
default: {
printf("Invalid choice\n");
break;
}
}
} while (choice != 5);
return 0;
}
void insert(int item) {
if ((front == 0 && rear == n - 1) || (front == rear + 1)) {
printf("Queue Overflow\n");
} else {
if (front == -1) {
front = 0;
}
if (rear == n - 1) {
rear = 0;
} else {
rear = rear + 1;
}
cqueue[rear] = item;
}
}
int del() {
if (front == -1) {
printf("Circular Queue Underflow\n");
return -1;
} else {
int item = cqueue[front];
if (front == rear) {
front = rear = -1;
} else if (front == n - 1) {
front = 0;
} else {
front = front + 1;
}
return item;
}
}
int peek() {
if (front == -1 || front == rear + 1) {
printf("Circular queue Underflow\n");
return -1;
} else {
return cqueue[front];
}
}
void display() {
int i;
printf("Circular queue elements:\n");
i = front;
if (front <= rear) {
while (i <= rear) {
printf("%d ", cqueue[i++]);
}
} else {
while (i <= n - 1) {
printf("%d ", cqueue[i++]);
}
i = 0;
while (i <= rear) {
printf("%d ", cqueue[i++]);
}
}
printf("\n");
}
OUTPUT:Enter the number of elements : 4
1.insert
2.del
3.peek
4.display
5.exit
Enter your choice : 1
Enter the element to be insert : 3
Enter your choice : 1
Enter the element to be insert : 4
Enter your choice : 1
Enter the element to be insert : 7
Enter your choice : 1
Enter the element to be insert : 6
Enter your choice : 1
Enter the element to be insert : 8
Queue Overflow
Enter your choice : 4
circular queue elements
3 4 7 6 Enter your choice : 3
The element at the peek of the circular queue = 3
Enter your choice : 2
The deleted element = 3
Enter your choice : 4
circular queue elements 4 7 6