Program to implement MIN Priority Queue

Priority Queue

Program to implement MIN Priority Queue

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 10

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

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

void display(struct queue *q)
{
int f,r;
for(f=q->front;f<=q->rear;f++)
{printf(“%dt”,q->arr[f]);
}
}

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

void main()
{
int ch,x;
struct queue q;
q.front=-1;
q.rear=-1;
while(1)
{
clrscr();
printf(“nMenun1. Pushn2. Popn3. Displayn4. Exit”);
printf(“nEnter your choice?”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“nEnter the number?”);
scanf(“%d”,&x);
push(&q,x);
break;
case 2:
x=pop(&q);
printf(“nPopped element is %d”,x);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
printf(“nInvalid choice!”);
}
getch();
}
}