/* ROLL NO:- 09BCE006
PRIORITY QUEUE
*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
void insert(void);
void del(void);
void disply(void);
struct node
{
int priority;
int value;
struct node *link;
}*first=NULL;
void main()
{
int n;
clrscr();
while(1)
{
cout<<"\n 1.insert \n 2.delete \n 3.display \n 4.Exit \n";
cin>>n;
switch(n)
{
case 1:insert();
break;
case 2:del();
break;
case 3:disply();
break;
case 4:exit(1);
default:cout<<"\n insert valid choice";
break;
}
}
}
void insert()
{
struct node *nw,*p;
int val,prior;
nw=(struct node *)malloc(sizeof(struct node));
cout<<"\n enter the value of node:";
cin>>val;
cout<<"\n enter the priority of node: ";
cin>>prior;
nw->value=val;
nw->priority=prior;
if(first== NULL || prior<first->priority)
{
nw->link=first;
first=nw;
}
else
{
p=first;
while(p->link != NULL && p->link->priority <=prior)
p=p->link;
nw->link=p->link;
p->link=nw;
}
}
void del()
{
struct node *nw;
if(first == NULL)
cout<<"Queue Underflow\n";
else
{
nw = first;
cout<<"Deleted item is: "<<nw->value;
first = first->link;
free(nw);
}
}
void disply()
{
struct node *ptr;
ptr = first;
if(first == NULL)
cout<<"Queue is empty\n";
else
{
cout<<"Queue is :\n\n";
cout<<"Priority\tItem\n";
while(ptr!=NULL)
{
cout<<ptr->priority<<"\t\t"<<ptr->value<<"\n";
ptr = ptr->link;
}
}
}