# Program to implement MAX Priority Queue

## Priority Queues

### Program to implement MAX Priority Queue

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 5

void display(struct queue *q);

struct queue
{int front,rear;
int arr[MAX];
};

void push(struct queue *q,int arr[],int n)
{
int i=0,j,f,r;
for(i=0;i<n;i++)
{if(q->front==-1)
{q->arr[0]=arr[i];
q->front=q->rear=0;
}
else
{f=q->front;
r=q->rear;
while(f<=r && q->arr[f]>arr[i])
f++;
for(j=r+1;j>=f;j–)
q->arr[j+1]=q->arr[j];
q->arr[f] = arr[i];
q->rear++;
}
}
}

void display(struct queue *q)
{
int f;
if(q->front==-1)
{printf(“nQueue is empty!”);
exit(1);
}
for(f=q->front;f<=q->rear;f++)
printf(“%dt”,q->arr[f]);
}

int pop(struct queue *q)
{if(q->front == -1)
{printf(“nQueue empty! Underflow.”);
return 0;
}
return (q->arr[q->front++]);
}

void main()
{
int arr[MAX],n,i,j,f;
struct queue q;
q.front = q.rear = -1;
clrscr();
printf(“nEnter total numbers?”);
scanf(“%d”,&n);
printf(“nEnter numbers?”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
push(&q,arr,n);
printf(“nDisplaying elements of MAX Priority queue:”);
display(&q);
printf(“nPop an element from MAX Priority queue:”);
i=pop(&q);
printf(“nPopped element is maximum element i.e. %d”,i);
printf(“nMax Priority Queue after pop operation:”);
display(&q);
getch();
}

error: Content is protected !!