Priority Queue , swap
May 18, 2012 at 3:59pm UTC
look at this code
I want to work program for the clinic will accept patients on the basis of the patient's condition , queue by linked list .
this is the code without loop and if statement , i must have if statement to check priority , if it is ==1 "Critical condition" and i must make swap , else add them to queue .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct personal_rec
{
string name;
int birth;
};
struct patient
{
int id;
int pr;
string status;
personal_rec person;
patient *next;
};
void enqueu ( patient *& , patient *&);
int main()
{
patient * front=NULL , * rear=NULL;
enqueu ( rear , front);
return 0 ;
}
void enqueu ( patient *& front , patient *& rear)
{
patient *n;
n=new patient;
cout<<"enter id" <<endl;
cin>>n->id;
cout<<"enter name" <<endl;
cin >> n->person.name;
cout<<"enter birth" <<endl;
cin >> n->person.birth;
cout<<"enter status" <<endl;
cin >> n->status;
cout<<"enter priority" <<endl;
cin>>n->pr;
n->next=NULL;
if (front==NULL)
{
front=n;
rear=n;}
else
if (n->pr==0)
{
rear->next=n;
rear=n;
}
else
{// here i need code for swapping
}
}
May 18, 2012 at 8:21pm UTC
If you were to actually write some code then explain why you don't think it's working the way you expect it to, you might get some responses.
"I need code" isn't a question.
May 18, 2012 at 8:53pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct personal_rec
{
string name;
int birth;
};
struct patient
{
int id;
int pr;
string status;
personal_rec person;
patient *next;
};
void enqueu ( patient *& , patient *&);
int main()
{
patient * front=NULL , * rear=NULL;
enqueu ( rear , front);
return 0 ;
}
void enqueu ( patient *& front , patient *& rear)
{
patient *n;
n=new patient;
cout<<"enter id" <<endl;
cin>>n->id;
cout<<"enter name" <<endl;
cin >> n->person.name;
cout<<"enter birth" <<endl;
cin >> n->person.birth;
cout<<"enter status" <<endl;
cin >> n->status;
cout<<"enter priority" <<endl;
cin>>n->pr;
n->next=NULL;
if (front==NULL)
{
front=n;
rear=n;}
else
if (n->pr==0)
{
rear->next=n;
rear=n;
}
else // here i need code for swapping when there is 3 nodes
{ patient *cur=front;
rear->next=rear;
rear=rear->next;
while (cur->next==rear)
{
cur->next=cur;
front->next=front;
front=n; // this code just for 3 nodes i need for more .
}
}
}
May 18, 2012 at 9:07pm UTC
I reposted the entire thing just so if anyone else has something to contribute they can see correctly formatted code. Also corrected the spelling of enqueue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct personal_rec
{
string name;
int birth;
};
struct patient
{
int id;
int pr;
string status;
personal_rec person;
patient *next;
};
void enqueue ( patient *& , patient *&);
int main()
{
patient * front=NULL , * rear=NULL;
enqueue ( rear , front);
return 0 ;
}
void enqueue ( patient *& front , patient *& rear)
{
patient *n;
n=new patient;
cout<<"enter id" <<endl;
cin>>n->id;
cout<<"enter name" <<endl;
cin >> n->person.name;
cout<<"enter birth" <<endl;
cin >> n->person.birth;
cout<<"enter status" <<endl;
cin >> n->status;
cout<<"enter priority" <<endl;
cin>>n->pr;
n->next=NULL;
if (front==NULL)
{
front=n;
rear=n;}
else if (n->pr==0)
{
rear->next=n;
rear=n;
}
else // here i need code for swapping when there is 3 nodes
{
// Find the first node in the list which has a priority less than the node
// we're inserting.
// insert the new node before that node. No "swapping" is needed.
}
}
May 18, 2012 at 9:49pm UTC
thanks alot
Topic archived. No new replies allowed.