Hi everybody,

I am currently working on an assignment on linked lists. I am stuck with parts of the project, and I would be very pleased if anyone could help me.

I am supposed to create a linked list of patient records. Each record is identified by a patient ID and contains patient name, age, and also blood glucose level. All of this is given via input from the user.

I have one main problem:

In this assignment, there should be functions for finding the highest and lowest age of the patients entered.
I can find the max and min ages - this works - BUT after having done that, the patient record for that particular patient need to be displayed, and this is where I get problems! How can I use the value of the new "max" to lookup the patient that has the maximum age? I have tried different ways of assigning max to temp->age and then trying to find ID - it does not give me any errors at compilation but it breaks down when I run the program.

NB I have omitted some of the functions in the code.

#include <iostream>
using namespace std;
#include <string>


// ------------- STRUCTURE -----------------------------
struct Patient_record // the basic building block of a linked list
{
	int ID;
	string name;
	int age;
	int glucose;
	struct Patient_record* next;
};

typedef struct Patient_record* recordptr;  
// define the type of the pointer to a node

// ---------- CLASS DEFINITION -----------------------------
class List
{
private:
	recordptr head;  // the head pointer of the linked list
public:
	List();
	~List();
	bool IsListEmpty();
	int Length();
	bool ListInsert(int ID);
	bool ListDelete(int ID);
	recordptr ListRetrieve(int ID);
	void PrintList();
	void ListAvg();
	void ListMax();
	void ListMin();
};

// ---------- CONSTRUCTOR ----------------------------
List::List():head(NULL)
{
} 

bool List::IsListEmpty()
{
return(head == NULL);
} 

// ---------- DESTRUCTOR ----------------------------
List::~List()
{
recordptr temp = head;
while (temp!=NULL)
{
  head = temp->next;
  temp->next = NULL;
  delete temp;
  temp = head;
  }
head = NULL;
} 

// ---------- FUNCTION Length ----------------------------
int List::Length()
{
recordptr temp = head;
int counter=0;
while (temp!=NULL)
  {
  counter++;
  temp=temp->next;
  }
return (counter);
} 

// ---------- FUNCTION ListInsert for adding patients ----------------
bool List::ListInsert(int patientID)
{ // start function 
recordptr temp = new (struct Patient_record);
if (temp==NULL)
  { 
  cout << "Not enough memory!" << endl;
  return(0);
  } 
else
  {
	temp->ID=patientID;
	temp->next=head;
	head=temp;
	
	cout<<"Enter name of patient: ";
    cin>>temp->name;
    cout<<"Enter age: ";
    cin>>temp->age;
    cout<<"Enter fasting blood glucose: ";
    cin>>temp->glucose;
	cout <<"\n\n";
   
  return (1);
  } 
} 

// ---------- FUNCTION PrintList --------------------------
void List::PrintList()
{
recordptr temp = head;
cout << "\n\n*******************" << endl;
cout << " List of patients:" << endl;
cout << "*******************" << endl;
while (temp!=NULL)
  {
	cout << "Patient ID: " << temp->ID << endl;
	cout << "Name      : " << temp->name << endl;
	cout << "Age       : " << temp->age << endl;
	cout << "Glucose   : " << temp->glucose << "\n\n";

	temp=temp->next;
}
}

// ---------- FUNCTION ListMax -------------------------
void List::ListMax()
{
  recordptr temp = head;
  int max=0;
  
  if(temp!=NULL)
	  max = temp->age;
  temp = temp->next;
  
  while (temp!=NULL)
    {
	  max = (max > temp->age)?max:temp->age;
     temp = temp->next;  
  }
  
  cout << "Highest age: " << max << "\n";
//  while (temp->age==max) I have tried adding this but it only 
// gives me errors when I run the program
//	  cout << "The age is : " << temp->age;
  }

and in main:

int main()
{
List l;
for(int i=0; i<3; i++)
  l.ListInsert(i);

l.PrintList();
l.ListMax(); // invoke to check for minimum value for first print of list 
return 0;
}

Thanks for your help,
Gro

You could create additional pointers to the linked list that you assign during the search functions (Pass the pointers as parameters).

Then, once you've searched, you can continue to bring up information from those particular patients through the pointers which are still referencing to the same area in memory.

Or, you could make your max function return the offset from the head of the list. So, when you want to look at that patient's info, you iterate through the list in a loop, then retrieve the appropriate information.

What would happen if you ran this code?

void List::ListMax()
{
    recordptr temp = head;
    recordptr max = head;
    int maxAge = 0;
    
    if(temp!=NULL)
        maxAge = temp->age;
        
    temp = temp->next;

    while (temp!=NULL)
    {
        if ( temp->age > maxAge)
        {
            maxAge = temp->age;
            max = temp;
        }
        temp = temp->next;  
    }
    if ( max != NULL )
        cout << max->age;
}

Thanks for your very rapid answers! I will try to implement it in the code and make it work.

Thank you
Gro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.