0% found this document useful (0 votes)
4 views5 pages

Implementing Priority Queue in C

This document contains a C program that implements a priority queue using arrays. It includes functions for enqueueing data with a priority, printing the queue, and dequeuing the highest priority element. The main function provides a menu for user interaction to insert data, display the queue, or delete elements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Implementing Priority Queue in C

This document contains a C program that implements a priority queue using arrays. It includes functions for enqueueing data with a priority, printing the queue, and dequeuing the highest priority element. The main function provides a menu for user interaction to insert data, display the queue, or delete elements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Priority Queue using Arrays

#include<stdio.h>

#define N 20

int Q[N],Pr[N];

int r = -1,f = -1;

void enqueue(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 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--) // checking for the priority

if(p>Pr[i])

Q[i+1] = Q[i];

Pr[i+1] = Pr[i];" organising elements

else

break;

Q[i+1] = data;

Pr[i+1] = p;

r++;

else

for(i = r;i>=f;i--)

if(p>Pr[i])//Check for priority

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 dequeue() //remove the data from front

if(f == -1)

printf("Queue is Empty");

else

printf("deleted Element = %d\t Its Priority = %d",Q[f],Pr[f]);

if(f==r)

f = r = -1;

else
f++;

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 \n3 for
Delete the data from the Queue\n0 for Exit");

scanf("%d",&opt);

switch(opt){

case 1:

printf("\nEnter the number of data");

scanf("%d",&n);

printf("\nEnter your data and Priority of data");

i=0;

while(i<n){

scanf("%d %d",&data,&p);

enqueue(data,p);

i++;

break;

case 2:

print();

break;

case 3:

dequeue();
break;

case 0:

break;

default:

printf("\nIncorrect Choice");

}while(opt!=0);

return 0;

You might also like