Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Program to implement MAX Priority Queue

Program to implement MAX Priority Queue

October 2, 2012 Comments Off on Program to implement MAX Priority Queue By admin

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();
}

 

Comments are closed
x Close

Like Us On Facebook

You cannot copy content of this page.