Tuesday, 23 September 2014

An easy way of making circular que by just keeping a track of number of times program goes to insertion loop and number of times it goes in deletion

int m=0;
int n=0;
#define max 4
struct queue{
int a[max];
int top,rear;
};
struct queue s;
isfull()
{
if(m-n==max){return 1;}
else return 0;
}
isempty()
{
if(m==n){return 1;}
else{return 0;}
}
insert()
{
if(!isfull())
{int val;
printf("Enter the value to be inserted\n");
scanf("%d",&val);
 m++;
s.rear=(s.rear+1)%max;
s.a[s.rear]=val;
}
else printf("Stack is full");
}
del()
{
if(!isempty())
{n++;
s.top=(s.top+1)%max;
}
else printf("Stack is empty");
}
display()
{
if(!isempty()){
int o=s.rear;
while(o!=s.top)
{
printf("%d\n",s.a[o]);
if(o==0){o=max-1;}
else o=o-1;
}
printf("%d",s.a[s.top]);
}
}
main()
{
s.top=0;
s.rear=-1;
int choice;
{
printf("1    INSERT\n2     DELETE\n3     DISPLAY\n");
while(1)
{
scanf("%d",&choice);
printf("\n");
switch(choice)
{
case 1: insert();break;
case 2: del();break;
case 3: display();break;
case 4: exit(2);break;
}
}
}
}

No comments:

Post a Comment