#include
#define MAX 5
struct que
{
int data[MAX];
int front,rear;
}q;
typedef struct que que;
int full(struct que(q))
{
if((q.rear+1)%MAX==(q.front))
return(1);
else
return(0);
}
int empty(struct que(q))
{
if(q.front==q.rear)
return(1);
else
return(0);
}
int qinsert(struct que *q,int num)
{
if(full(*q))
{
printf("\n que is full");
return(1);
}
q->rear=(q->rear+1)%MAX;
q->data[q->rear]=num;
// return(1);
}
int qdelete(struct que *q)
{
int num;
if(empty(*q))
{
printf("\n que is empty");
return(-1);
}
q->front=(q->front+1)%MAX;
num=q->data[q->front];
return(num);
}
int qdisplay(struct que q)
{
int i;
if(empty(q))
{
printf("\n que is empty");
return(1);
}
else
{
printf(" the contents of que : ");
for(i=(q.front+1)%MAX;i!=q.rear;i=(i+1)%MAX)
{
printf(" %5d",q.data[i]);
}
printf("%5d",q.data[i]);
}
}
int main()
{
int i,done=0,ch,num;
que q;
q.front=MAX-1;
q.rear=MAX-1;
while(!done)
{
printf("\n 1.to qinsert"
"\t 2.qdelete"
"\t 3.qdisplay"
"\n 4.exit"
"\n Enter ur choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the element to be inserted :");
scanf("%d",&num);
qinsert(&q,num);
break;
case 2:
num=qdelete(&q);
if(num!=-1)
printf("\n deleted element: %d",num);
break;
case 3:
qdisplay(q);
break;
case 4:
default:done=1;
break;
}
}
}
No comments:
Post a Comment