C - Program to Implement Priority Queue
#include <stdio.h>
#include <conio.h>
typedef struct node
{
int priority;
char name[20];
struct node *next;
}node;
void initialize(node **h)
{
*h=NULL;
}
int empty(node *h)
{
if(h==NULL)
return 1;
return 0;
}
void insert(node **h,char name[], int priority)
{
node *p,*q;
p=(node*)malloc(sizeof(node));
strcpy(p->name,name);
p->priority=priority;
p->next=NULL;
if(*h==NULL)//if the queue is empty
*h=p;
else
{
if(priority > (*h)->priority)//highest priority element
{
p->next=*h;
*h=p;
}
else
{ //locate the point of insertion
q=*h;
while(q->next !=NULL && priority<=q->next->priority)
q=q->next;
//insert
p->next=q->next;
q->next=p;
}
}
}
node *Delete(node **h)
{
node *p;
p=*h;
*h=p->next;
return(p);
}
void display(node *h)
{
printf("\n Name Priority");
while(h!=NULL)
{
printf("\n%20s %d",h->name,h->priority);
switch(h->priority)
{
case 1: printf(" General Checkup");break;
case 2: printf(" Non-serious");break;
case 3: printf(" Serious");break;
default:printf(" Unknown");
}
h=h->next;
}
}
void main()
{
node *head=NULL;
node *p;
int priority,op;
char name[20];
clrscr();
do
{
printf("\n\n1)Enter a New Patient");
printf("\n2)Service the next patient");
printf("\n3)Display the list of patients");
printf("\n4)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nName of the patient : ");
flushall();
gets(name);
printf("\nPriority(1- General checkup,2- non-serious,3- serious ): ");
scanf("%d",&priority);
insert(&head,name,priority);
break;
case 2: if(!empty(head))
{
p=Delete(&head);
printf("\n%s %d",p->name,p->priority);
switch(p->priority)
{
case 1: printf(" General Checkup");break;
case 2: printf(" Non-serious");break;
case 3: printf(" Serious");break;
default:printf(" Unknown");
}
}
else
printf("\nQueue is empty");
break;
case 3: display(head);break;
}
}while(op!=4);
}
#include <conio.h>
typedef struct node
{
int priority;
char name[20];
struct node *next;
}node;
void initialize(node **h)
{
*h=NULL;
}
int empty(node *h)
{
if(h==NULL)
return 1;
return 0;
}
void insert(node **h,char name[], int priority)
{
node *p,*q;
p=(node*)malloc(sizeof(node));
strcpy(p->name,name);
p->priority=priority;
p->next=NULL;
if(*h==NULL)//if the queue is empty
*h=p;
else
{
if(priority > (*h)->priority)//highest priority element
{
p->next=*h;
*h=p;
}
else
{ //locate the point of insertion
q=*h;
while(q->next !=NULL && priority<=q->next->priority)
q=q->next;
//insert
p->next=q->next;
q->next=p;
}
}
}
node *Delete(node **h)
{
node *p;
p=*h;
*h=p->next;
return(p);
}
void display(node *h)
{
printf("\n Name Priority");
while(h!=NULL)
{
printf("\n%20s %d",h->name,h->priority);
switch(h->priority)
{
case 1: printf(" General Checkup");break;
case 2: printf(" Non-serious");break;
case 3: printf(" Serious");break;
default:printf(" Unknown");
}
h=h->next;
}
}
void main()
{
node *head=NULL;
node *p;
int priority,op;
char name[20];
clrscr();
do
{
printf("\n\n1)Enter a New Patient");
printf("\n2)Service the next patient");
printf("\n3)Display the list of patients");
printf("\n4)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nName of the patient : ");
flushall();
gets(name);
printf("\nPriority(1- General checkup,2- non-serious,3- serious ): ");
scanf("%d",&priority);
insert(&head,name,priority);
break;
case 2: if(!empty(head))
{
p=Delete(&head);
printf("\n%s %d",p->name,p->priority);
switch(p->priority)
{
case 1: printf(" General Checkup");break;
case 2: printf(" Non-serious");break;
case 3: printf(" Serious");break;
default:printf(" Unknown");
}
}
else
printf("\nQueue is empty");
break;
case 3: display(head);break;
}
}while(op!=4);
}