6_CircularQueue
6_CircularQueue
6. 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<stdio.h>
#include<stdlib.h>
#define MAX 5
void Enqueue();
void Dequeue();
void Display();
int main()
{
int choice;
while(1)
{
printf("\nCIRCULAR QUEUE OPERATIONS USING ARRAY\n\n");
printf("1.INSERT\n");
printf("2.DELETE\n");
printf("3.DISPLAY\n");
printf("4.EXIT \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:Enqueue();
break;
case 2:Dequeue();
break;
case 3:Display();
break;
case 4:exit(1);
default:printf("\nInvalid Choice\n");
}
}
}
void Enqueue()
{
int item;
if ((front == 0 && rear == MAX - 1) || front == rear + 1)
{ // front==rear + 1 occurs when an element is inserted at
// 0th position due to circular increment and rear value is just
// one less than front i.te front = 1 and rear =0
printf("\nCircular Queue is Full \n");
}
else
{
printf("Enter the element to insert into the Circular Queue :\n ");
scanf("%d", &item);
if (front == -1)
front = 0;
rear = (rear + 1) % MAX;
CQueue[rear] = item;
}
}
void Dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nCircular Queue is Underflow\n");
}
else if(front==rear) //Q has only one element, so we reset the
// queue after dequeue operation
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=(front+1)%MAX;
}
}
void Display()
{
int i;
if (front == -1 && rear == -1)
{
printf("\nThe Circular Queue is Empty \n");
}
else
{
printf("\nElements in a Circular Queue are :");
if (front > rear)
{
for (i = front; i < MAX; i++)
{
printf("%d ", CQueue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", CQueue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", CQueue[i]);
}
}
}
/* OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
10
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
20
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
30
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
40
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
50
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
90
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
100
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 4
*/
/*
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void Enqueue();
void Dequeue();
void Display();
int main()
{
int choice;
while(1)
{
printf("\nCIRCULAR QUEUE OPERATIONS USING ARRAY\n\n");
printf("1.INSERT\n");
printf("2.DELETE\n");
printf("3.DISPLAY\n");
printf("4.EXIT \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:Enqueue();
break;
case 2:Dequeue();
break;
case 3:Display();
break;
case 4:exit(1);
default:printf("\nInvalid Choice\n");
}
}
}
void Enqueue()
{
int item;
if ((front == 0 && rear == MAX - 1) || front == rear + 1)
{ // front==rear + 1 occurs when an element is inserted at
// 0th position due to circular increment and rear value is just
// one less than front i.te front = 1 and rear =0
printf("\nCircular Queue is Full \n");
}
else
{
printf("Enter the element to insert into the Circular Queue :\n ");
scanf("%d", &item);
if (front == -1)
front = 0;
rear = (rear + 1) % MAX;
CQueue[rear] = item;
}
}
void Dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nCircular Queue is Underflow\n");
}
else if(front==rear) //Q has only one element, so we reset the
// queue after dequeue operation
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=(front+1)%MAX;
}
}
void Display()
{
int i;
if (front == -1 && rear == -1)
{
printf("\nThe Circular Queue is Empty \n");
}
else
{
printf("\nElements in a Circular Queue are :");
if (front > rear)
{
for (i = front; i < MAX; i++)
{
printf("%d ", CQueue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", CQueue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", CQueue[i]);
}
}
}
/* OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
20
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
30
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
40
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
50
CIRCULAR QUEUE OPERATIONS USING ARRAY
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
90
CIRCULAR QUEUE OPERATIONS USING ARRAY
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
100
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 4
*/