every menu works except the menu 4) where we have to move the patients with high
ID: 674918 • Letter: E
Question
every menu works except the menu 4) where we have to move the patients with highest point to the end
can you fix that for me and kindly check before sending the codes
thanks
#include <iostream>
#include<iostream>
#include <stdlib.h>
#include <string>
using namespace std;
typedef struct Node
{
string Name;
int ID;
int age;
char smoker;
char HBP;
char HFD;
int Points;
Node *ptr;
};
Node *startPtr = NULL;
void getInfo(Node **root)
{
Node *p;
int agePoints, hbpPoints, hfdPoints, smkPoints;
for (int i = 0;i <2; i++)
{
p=new Node;
cout<<"Enter details of patient "<<(i+1)<<": "<<endl;
cout << " Please enter the name of the patient: ";
cin>>p->Name;
cout << " Please enter the ID: ";
cin >> p->ID;
cout << " Please enter the age of the patient: ";
cin >> p->age;
cout << " ts he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " toes he/she have high blood pressure? (y/n): ";
cin >> p->HBP;
cout << " Does he/she have a high fat diet? (y/n): ";
cin >> p->HFD;
cout << endl;
p->ptr = NULL;
if (p-> age < 18)
{
agePoints = 2;
}
else if (p->age > 18 && p->age <30)
{
agePoints = 3;
}
else
{
agePoints = 4;
}
if (p->smoker == 'Y' || p->smoker == 'y')
{
p->smoker = 'Y';
smkPoints = 4;
}
else
{
p->smoker = 'N';
smkPoints = 0;
}
if (p->HBP == 'Y' || p->HBP == 'y')
{
p->HBP = 'Y';
hbpPoints = 4;
}
else
{
p->HBP = 'N';
hbpPoints = 0;
}
if (p->HFD == 'Y' || p->HFD == 'y')
{
p->HFD = 'Y';
hfdPoints = 1;
}
else
{
p->HFD = 'N';
hfdPoints = 0;
}
p->Points = agePoints + smkPoints + hbpPoints + hfdPoints;
if (*root == NULL)
{
*root = p;
}
else
{
Node *p1 = *root;
while (p1->ptr != NULL)
p1 = p1->ptr;
p1->ptr = p;
}
}
}
void displayPatient(Node *p)
{
if(p!=NULL)
{
cout<<endl;
cout << "Patient id: " <<p->ID <<endl;
cout << " Name : " << p->Name << endl;
cout << " Age : " << p->age << endl;
cout << " Smoker : " << p->smoker << endl;
cout << " Blood Pressure : " << p->HBP << endl;
cout << " Fat Diet : " << p->HFD << endl;
cout << " Points : " << p->Points << endl;
}
}
void displayAllPatients(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
displayPatient(temp);
temp = temp->ptr;
}
}
void smokers(Node *root)
{
Node *temp = root;
int count = 0;
while(temp != NULL){
if(temp->smoker == 'Y')
count++;
temp = temp->ptr;
}
cout<<count<<endl;
}
void moveHighestPatientToEnd(Node **root)
{
Node *temp, *prevNode, *highestPointsNode, *prevHighestPointsNode;
prevNode = prevHighestPointsNode = highestPointsNode = temp = *root;
while(temp != NULL)
{
if(highestPointsNode->Points > temp->Points)
{
prevHighestPointsNode = prevNode;
highestPointsNode = temp;
}
prevNode = temp;
temp = temp->ptr;
}
prevHighestPointsNode = highestPointsNode->ptr;
temp = *root;
while(temp->ptr != NULL)
{
temp =temp->ptr;
}
temp->ptr = highestPointsNode;
highestPointsNode->ptr = NULL;
}
void highestPoint(Node *root)
{
Node *temp, *highestPointsNode;
highestPointsNode = temp = root;
while(temp != NULL)
{
if(highestPointsNode->Points < temp->Points)
highestPointsNode = temp;
temp = temp->ptr;
}
displayPatient(highestPointsNode);
}
int main()
{
int choice;
struct Node *patient = NULL, *previous = NULL;
struct Node *root = NULL;
getInfo(&root);
while(1)
{
cout<<" Patient Menu ";
cout<<"============================================= ";
cout<<"1. Display the patients ";
cout<<"2. Display total number of smokers ";
cout<<"3. Search for patient with highest point ";
cout<<"4. Move the patient with highest point to the end. ";
cout<<"5. Exit the program ";
cout<<"============================================= ";
cout<<"Enter your selection: ";
cin>>choice;
switch(choice)
{
case 1:
displayAllPatients(root);
break;
case 2:
cout<<"The number of smokers are: ";
smokers(root);
cout<<endl;
break;
case 3:
highestPoint(root);
break;
case 4:
moveHighestPatientToEnd(&root);
cout<<endl;
break;
case 5:
system("pause");
return 0;
default: cout<<"Invalid choice.";
}
}
}
Explanation / Answer
void moveHighestPatientToEnd(Node **root)
{
Node *temp, *prevNode, *highestPointsNode, *prevHighestPointsNode;
prevNode = prevHighestPointsNode = highestPointsNode = temp = *root;
while(temp != NULL)
{
if(highestPointsNode->Points > temp->Points)
{
prevHighestPointsNode = prevNode;
highestPointsNode = temp;
}
prevNode = temp;
temp = temp->ptr;
}
prevHighestPointsNode->ptr = highestPointsNode->ptr;
temp = *root;
while(temp->ptr != NULL)
{
temp =temp->ptr;
}
temp->ptr = highestPointsNode;
highestPointsNode->ptr = NULL;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.