#include"stdio.h"
#include"conio.h"
#define MAX 20
#define TRUE 1
#define FALSE 0
struct Queue
{
int front;
int rear;
int items[MAX];
}cq;
void insert(struct Queue *cq, int );
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)
{
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
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);
}
printf(" the queue items are:\n");
display(&cq);
break;
case 3:
x=remov(&cq);
printf("\n the value of x=%d",x);
break;
case 2:
display(&cq);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
}
}
getch();
}
void insert(struct Queue *cq,int y)
{
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;
}
int remov(struct Queue *cq)
{
int x;
if(empty(cq))
{
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];
}
return(x);
}
int empty(struct Queue *cq)
{
if(cq->rear==cq->front)
return(TRUE);
else
return(FALSE);
}
void display(struct Queue *cq)
{
int i,x;
if(cq->rear==cq->front)
{
printf("\n queue is empty");
getch();
exit(0);
}
if(cq->front==MAX-1)
{
for(i=0;i<= cq->rear;i++)
{
x=cq->items[i];
printf("\n %d",x);
}
getch();
}
else
for(i=cq->front;i< cq->rear;i++)
{
x=cq->items[i+1];
printf("\n %d",x);
}
}
No comments:
Post a Comment