#include<iostream.h>
#include<conio.h>
class pqueue
{
pqueue *pre;
int data;
int pri;
int num,prio;
pqueue *next;
void add(int,int);
int remove(void);
public :
void display(void);
void priority(void);
};
pqueue *front;
pqueue *rear;
void pqueue :: add(int num,int pri)
{
if((front==NULL) && (rear==NULL))
{
front=rear= new pqueue;
rear->next=NULL;
rear->data=num;
rear->pri=pri;
rear->pre=NULL;
}
else
{
rear->next= new pqueue;
rear->next->pre= rear;
rear->next->data = num;
rear->next->next=NULL;
rear->next->pri=pri;
rear = rear->next;
}
}
void pqueue :: display(void)
{
pqueue *q = front;
if(front==NULL && rear== NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
while(q !=NULL)
{
cout<<"Numbers are:"<<endl<< q->data<<endl;
q = q->next;
}
}
}
int pqueue :: remove (void)
{
int result;
// queue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"Queue is empty"<<endl;
return -32767;
}
result = front->data;
front = front->next;
front->pre = NULL;
if(front != NULL)
{
delete (rear->next);
rear ->next = NULL;
}
else
{
delete (front);
front=NULL;
}
return result;
}
void pqueue :: priority(void)
{
if(front==NULL && rear == NULL)
{
cout<<"Queue is Empty" <<endl;
char ch='y';
cout<<"Do you want to add new?"<<endl;
cin>>ch;
while(ch=='y')
{
cout<<"Enter the data "<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
break;
}
}
else
{
if( rear->pri >= front->pri)
{
cout<<"Enter The data"<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
}
else
{
int r= remove();
cout<<"Removed = "<<r<<endl;
}
}
}
void main()
{
clrscr();
int num;
char choice='y';
pqueue q;
front=rear=NULL;
int option;
while(choice == 'y')
{
cout<<"1.Priority operation"<<endl;
cout<<"2.Display"<<endl;
cout<<"enter the option"<<endl;
cin>>option;
switch(option)
{
case 1:
{
q.priority();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 2:
{
q.display();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 5:
{
int r = q.remove();
cout<<"Removed ="<<r<<endl;
cout<<"Do you want to cnt....?"<<endl;
break;
} */
default:
{
cout<<"Wrong input"<<endl;
cout<<"Do you want to cont...?"<<endl;
break;
}
}
choice=getch();
}
}