In: Computer Science
write C program to implement the priority queue with the operation insert
#include<stdio.h>
#define N 20
int Q[N],Pr[N];
int r = -1,f = -1;
void enque(int data,int p)//Enqueue function to insert data and its priority in queue
{
int i;
if((f==0)&&(r==N-1)) //Check if Queue is full
printf("Queue is full");
else
{
if(f==-1)//if Queue is empty
{
f = r = 0;
Q[r] = data;
Pr[r] = p;
}
else if(r == N-1)//if there there is some elemets in Queue
{
for(i=f;i<=r;i++) { Q[i-f] = Q[i]; Pr[i-f] = Pr[i]; r = r-f; f = 0; for(i = r;i>f;i--)
{
if(p>Pr[i])
{
Q[i+1] = Q[i];
Pr[i+1] = Pr[i];
}
else
break;
Q[i+1] = data;
Pr[i+1] = p;
r++;
}
}
}
else
{
for(i = r;i>=f;i--)
{
if(p>Pr[i])
{
Q[i+1] = Q[i];
Pr[i+1] = Pr[i];
}
else
break;
}
Q[i+1] = data;
Pr[i+1] = p;
r++;
}
}
}
void print() //print the data of Queue
{
int i;
for(i=f;i<=r;i++)
{
printf("\nElement = %d\tPriority = %d",Q[i],Pr[i]);
}
}
int main()
{
int opt,n,i,data,p;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n0 for Exit \n");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data and Priority of data \n");
i=0;
while(i<n){
scanf("%d %d",&data,&p);
enque(data,p);
i++;
}
break;
case 2:
print();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");
}
}while(opt!=0);
return 0;
}
Output screen:
if you have any doubt regarding this question then fell free to ask in comment section.