#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
void enqueue();
int dequeue();
void display();
int queue[maxsize];
int rear = -1;
int front = -1;
main() {
char choice;
int y;
printf("*****INSTRUCTIONS*****\nE - Enter\nX - Delete\nD - Display\nQ - Quit\n");
while (1) {
printf("\nEnter choice");
scanf("%s", & choice);
switch (choice) {
case 'E':
enqueue();
break;
case 'X':
dequeue();
break;
case 'D':
display();
break;
case 'Q':
exit(1);
break;
}
}
}
void enqueue() {
int x;
printf("Enter element:");
scanf("%d", & x);
if (front == -1 && rear == -1) {
front = 0, rear = 0;
queue[rear] = x;
} else if ((rear + 1) % maxsize == front) {
printf("Queue is overflow");
} else {
rear = (rear + 1) % maxsize;
queue[rear] = x;
}
}
int dequeue() {
int x;
x = queue[front];
if (front == -1 && rear == -1) {
printf("queue is underflow");
} else if (front == rear) {
printf("The deleted element is %d\n", x);
front = -1, rear = -1;
} else {
printf("The deleted element is %d\n", x);
front = (front + 1) % maxsize;
}
}
void display() {
if (front == -1 && rear == -1) {
printf("queue is empty");
} else {
int i = front;
printf("Elements in the circular queue are:");
while (i <= rear) {
printf("%d\t", queue[i]);
if (i == rear) {
break;
}
i = (i + 1) % maxsize;
}
}
}
Note: Need to be arranged in compiler after copied
OutPut:
*****INSTRUCTIONS*****
E - Enter
X - Delete
D - Display
Q - Quit
Enter choiceE
Enter element:3
Enter choiceE
Enter element:2
Enter choiceE
Enter element:1
Enter choiceX
The deleted element is 3
Enter choiceD
Elements in the circular queue are:2 1
Enter choiceQ