Wednesday, 15 February 2017

Implement Priority Queue As ADT Using Single Linked List for Patients in Hospital

Implement priority queue as ADT using single linked list for servicing patients in an hospital with priorities as i) Serious (top priority) ii) medium illness (medium priority) iii) General (Least priority).

#include<iostream>
#include<stdlib.h>
using namespace std;
class Queue
{
public:

struct node
          {
       int data;
       char name[10];
       struct node* next;
   };

struct node *head;

Queue()
       {

head=NULL;
}//Constructor

~Queue();
void Enqueue();
void Deque();
int Qempty();
void PrintTop();
void PrintLast();
};

void Queue::Enqueue()
{

struct node *p,*q;
p=new node;
cout<<"ENTER PATIENT NAME\t";
cin>>p->name;
cout<<"\nENTER PRIRITOY\t";
cin>>p->data;

p->next=NULL;
if(head==NULL)
       {
head=p;
}

else
        {
if(p->data>head->data)
               {
p->next=head;
head=p;
}

else
                {

q=head;
while(q->next!=NULL && (p->data <= (q->next->data)))
q=q->next;

p->next=q->next;
q->next=p;
}
}
}

void Queue::Deque()
{

struct node *p;
p=head;

head=p->next;
cout<<"\nDequed element id\t"<<p->data;
cout<<"\nDequed element name\t"<<p->name;
delete p;
}

int Queue::Qempty()
{

if(head==NULL)
       {
return (1);
}

else
return (0);
}

void Queue::PrintTop()
{

cout<<head->name<<"\t"<<head->data;
}

void Queue::PrintLast()
{

struct node *q;
int x;
q=head;
while(q->next!=NULL)
       {

q=q->next;
}

cout<<q->name<<"\t"<<q->data;
}

Queue::~Queue()
{

struct node *p;
p=head;
while(head!=NULL)
      {

p=head;
head=head->next;
delete p;
}
}

int main()
{
Queue s;
int x,n,ch,i;
struct node *head;
char pname[10];
do
{
cout<<"\n\tMENU\n1.CREATE\n2.INSERT\n3.DELETE\n4.PRINTT0P\n5.PRINTLAT\n6.EXIT";
cout<<"\nENTER YOUR CHOICE\t";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nENTER NO. OF ENTRIES\t";
cin>>n;
for(i=0;i<n;i++)
{
s.Enqueue();
}
break;
case 2:
s.Enqueue();
break;
case 3:if(s.Qempty())
{
cout<<"\nQUEUE IS EMPTY\nELEMENTS CAN NOT BE DELETED";
}
s.Deque();

      break;
case 4:cout<<"ELEMENT AT THE FRONT OF QUEUE IS";
s.PrintTop();
//cout<<"\t"<<x;
break;
case 5:cout<<"ELEMENT AT THE REAR OF QUEUE IS";
s.PrintLast();
break;
case 6:cout<<"PROGRAM ENDING\n";
exit(0);
}
}while(ch<7);
}

2 comments: