Lab program-6
Design, Develop and Implement 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 10
int front=0,rear=-1;
char q[10];
void insert();
void delet();
void display();
void main()
{
int ch;
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Invalid option\n");
}
}
}
void insert()
{
char x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
fflush(stdin);
scanf("%c",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
char a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
exit(1);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%c\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
exit(1);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%c",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%c",q[j]);
printf("\nrear is at %c\n",q[rear]);
printf("\nfront is at %c\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%c",q[i]);
}
printf("\nrear is at %c\n",q[rear]);
printf("\nfront is at %c\n",q[front]);
}
printf("\n");
}
#include<stdlib.h>
#define max 10
int front=0,rear=-1;
char q[10];
void insert();
void delet();
void display();
void main()
{
int ch;
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Invalid option\n");
}
}
}
void insert()
{
char x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
fflush(stdin);
scanf("%c",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
char a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
exit(1);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%c\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
exit(1);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%c",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%c",q[j]);
printf("\nrear is at %c\n",q[rear]);
printf("\nfront is at %c\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%c",q[i]);
}
printf("\nrear is at %c\n",q[rear]);
printf("\nfront is at %c\n",q[front]);
}
printf("\n");
}