//Priority queue using array
#include<stdio.h>
#include<conio.h>
struct queue
{
int a[5];
int f;
int r;
};
main()
{
int item,i,pr,j,f=0;
struct queue q[5];
void insert(struct queue *q,int item);
int delet(struct queue *q);
q[0].f=q[0].r=-1;
q[1].f=q[1].r=-1;
q[2].f=q[2].r=-1;
q[3].f=q[3].r=-1;
q[4].f=q[4].r=-1;
while(1)
{
printf("\n Do you insert element in queue if Yes press \"1\" otherwise press \"0\" :");
scanf("%d",&i);
if(i==1)
{
printf("\nEnter the priority(0-4) of the queue.");
scanf("%d",&pr);
if(pr<5)
{
printf("\nEnter the element to be insert in queue: ");
scanf("%d",&item);
insert(&q[pr],item);
}
else
printf("\nSorry You have entered invalid priority");
}
else
break;
}
while(1)
{
printf("\n Do you delete element from queue if Yes press \"1\" otherwise press \"0\" :");
scanf("%d",&i);
if(i==1)
{
printf("\nEnter the priority(0-4) of the queue.");
scanf("%d",&pr);
if(pr<5)
{
f=0;
for(j=0;j<=pr-1;j++)
{
if(q[j].f!=-1)
{
f=1;
break;
}
}
if(f==0)
{
item=delet(&q[pr]);
if(item==-1000)
{
printf("\nUnderflow");
}
else
printf("\nitem deleted from queue is: %d",item);
}
else
printf("\nSorry items in the higher priority queue are not processed still.");
}
else
printf("\nSorry You have entered invalid priority");
}
else
break;
}
getch();
return 0;
}
void insert(struct queue *q,int item)
{
if (q->f==0&&q->r==4||q->f==q->r+1)
{
printf("\nOverflow");
return;
}
if(q->f==-1&&q->r==-1)
{
q->f=0;
q->r=0;
}
else if (q->r==4&&q->f!=0)
q->r=0;
else
q->r=q->r+1;
q->a[q->r]=item;
}
int delet(struct queue *q)
{
int item;
if(q->f==-1)
return -1000;
item=q->a[q->f];
if(q->f==q->r)
q->f=q->r=-1;
else if(q->f==4)
q->f=0;
else
q->f=q->f+1;
return item;
}
#include<stdio.h>
#include<conio.h>
struct queue
{
int a[5];
int f;
int r;
};
main()
{
int item,i,pr,j,f=0;
struct queue q[5];
void insert(struct queue *q,int item);
int delet(struct queue *q);
q[0].f=q[0].r=-1;
q[1].f=q[1].r=-1;
q[2].f=q[2].r=-1;
q[3].f=q[3].r=-1;
q[4].f=q[4].r=-1;
while(1)
{
printf("\n Do you insert element in queue if Yes press \"1\" otherwise press \"0\" :");
scanf("%d",&i);
if(i==1)
{
printf("\nEnter the priority(0-4) of the queue.");
scanf("%d",&pr);
if(pr<5)
{
printf("\nEnter the element to be insert in queue: ");
scanf("%d",&item);
insert(&q[pr],item);
}
else
printf("\nSorry You have entered invalid priority");
}
else
break;
}
while(1)
{
printf("\n Do you delete element from queue if Yes press \"1\" otherwise press \"0\" :");
scanf("%d",&i);
if(i==1)
{
printf("\nEnter the priority(0-4) of the queue.");
scanf("%d",&pr);
if(pr<5)
{
f=0;
for(j=0;j<=pr-1;j++)
{
if(q[j].f!=-1)
{
f=1;
break;
}
}
if(f==0)
{
item=delet(&q[pr]);
if(item==-1000)
{
printf("\nUnderflow");
}
else
printf("\nitem deleted from queue is: %d",item);
}
else
printf("\nSorry items in the higher priority queue are not processed still.");
}
else
printf("\nSorry You have entered invalid priority");
}
else
break;
}
getch();
return 0;
}
void insert(struct queue *q,int item)
{
if (q->f==0&&q->r==4||q->f==q->r+1)
{
printf("\nOverflow");
return;
}
if(q->f==-1&&q->r==-1)
{
q->f=0;
q->r=0;
}
else if (q->r==4&&q->f!=0)
q->r=0;
else
q->r=q->r+1;
q->a[q->r]=item;
}
int delet(struct queue *q)
{
int item;
if(q->f==-1)
return -1000;
item=q->a[q->f];
if(q->f==q->r)
q->f=q->r=-1;
else if(q->f==4)
q->f=0;
else
q->f=q->f+1;
return item;
}