Write a C Program to simulate the working of a circular queue of integers using an array.Provide the following operations:
a. Insert
b. Delete
c. Display
Program:
#include"stdio.h"
#include"conio.h"
#define MAX 20 /*definition section*/
#define TRUE 1
#define FALSE 0
struct Queue /*queue declaration*/
{
int front;
int rear;
int items[MAX];
int count;
}cq;
void insert(struct Queue *cq, int ); /*function prototype declarations*/
void display(struct Queue *cq);
int remov(struct Queue *cq);
int empty(struct Queue *cq);
main()
{
int i,n;
int choice,x;
cq.rear=cq.front=MAX-1;
clrscr();
printf("\n1.INSERT\n2.DISPLAY\n3.REMOVE\n");
while(1) /*using while loop*/
{
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice) /*switch statement*/
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i< n;i++)
{
scanf("%d",&x);
insert(&cq,x); /*insert function call*/
}
printf(" the queue items are:\n");
display(&cq); /*display function call*/
break;
case 2:
display(&cq);
break;
case 3:
x=remov(&cq); /*remov function call*/
printf("\n the value of x=%d",x);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
} /*end switch*/
} /*end while*/
getch();
}
void insert(struct Queue *cq,int y) /*insert function definition*/
{
if(cq->rear==MAX-1)
cq->rear=0;
else
cq->rear=cq->rear+1;
if(cq->rear==cq->front)
{
printf("Queue overflow");
getch();
exit(0);
}
cq->items[cq->rear]=y;
cq->count++;
}
int remov(struct Queue *cq) /*remov function definition*/
{
int x;
if(empty(cq)) /*empty function call*/
{
printf("\n queue underflow");
getch();
exit(1);
}
if(cq->front==MAX-1)
{
cq->front=0;
x=cq->items[cq->front];
}
else
{
cq->front=cq->front+1;
x=cq->items[cq->front];
}
cq->count--;
return(x);
}
int empty(struct Queue *cq) /*empty function definition*/
{
if(cq->rear==cq->front)
return(TRUE);
else
return(FALSE);
}
void display(struct Queue *cq) /*display function definition*/
{
int i,j,x;
if(empty(cq))
{
printf("\n queue is empty");
getch();
exit(0);
}
printf("\n The items of Queue are :");
j=(cq->front+1)%MAX;
for(i=0;i< cq->count;i++)
{
x=cq->items[j];
printf("\n %d",x);
j=(j+1)%MAX;
}
getch();
}
No comments:
Post a Comment