Looking up and displaying values in linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2005
Posts: 5
Reputation: gropedersen is an unknown quantity at this point 
Solved Threads: 0
gropedersen gropedersen is offline Offline
Newbie Poster

Looking up and displaying values in linked lists

 
0
  #1
Mar 31st, 2006
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.

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4.  
  5.  
  6. // ------------- STRUCTURE -----------------------------
  7. struct Patient_record // the basic building block of a linked list
  8. {
  9. int ID;
  10. string name;
  11. int age;
  12. int glucose;
  13. struct Patient_record* next;
  14. };
  15.  
  16. typedef struct Patient_record* recordptr;
  17. // define the type of the pointer to a node
  18.  
  19. // ---------- CLASS DEFINITION -----------------------------
  20. class List
  21. {
  22. private:
  23. recordptr head; // the head pointer of the linked list
  24. public:
  25. List();
  26. ~List();
  27. bool IsListEmpty();
  28. int Length();
  29. bool ListInsert(int ID);
  30. bool ListDelete(int ID);
  31. recordptr ListRetrieve(int ID);
  32. void PrintList();
  33. void ListAvg();
  34. void ListMax();
  35. void ListMin();
  36. };
  37.  
  38. // ---------- CONSTRUCTOR ----------------------------
  39. List::List():head(NULL)
  40. {
  41. }
  42.  
  43. bool List::IsListEmpty()
  44. {
  45. return(head == NULL);
  46. }
  47.  
  48. // ---------- DESTRUCTOR ----------------------------
  49. List::~List()
  50. {
  51. recordptr temp = head;
  52. while (temp!=NULL)
  53. {
  54. head = temp->next;
  55. temp->next = NULL;
  56. delete temp;
  57. temp = head;
  58. }
  59. head = NULL;
  60. }
  61.  
  62. // ---------- FUNCTION Length ----------------------------
  63. int List::Length()
  64. {
  65. recordptr temp = head;
  66. int counter=0;
  67. while (temp!=NULL)
  68. {
  69. counter++;
  70. temp=temp->next;
  71. }
  72. return (counter);
  73. }
  74.  
  75. // ---------- FUNCTION ListInsert for adding patients ----------------
  76. bool List::ListInsert(int patientID)
  77. { // start function
  78. recordptr temp = new (struct Patient_record);
  79. if (temp==NULL)
  80. {
  81. cout << "Not enough memory!" << endl;
  82. return(0);
  83. }
  84. else
  85. {
  86. temp->ID=patientID;
  87. temp->next=head;
  88. head=temp;
  89.  
  90. cout<<"Enter name of patient: ";
  91. cin>>temp->name;
  92. cout<<"Enter age: ";
  93. cin>>temp->age;
  94. cout<<"Enter fasting blood glucose: ";
  95. cin>>temp->glucose;
  96. cout <<"\n\n";
  97.  
  98. return (1);
  99. }
  100. }
  101.  
  102. // ---------- FUNCTION PrintList --------------------------
  103. void List::PrintList()
  104. {
  105. recordptr temp = head;
  106. cout << "\n\n*******************" << endl;
  107. cout << " List of patients:" << endl;
  108. cout << "*******************" << endl;
  109. while (temp!=NULL)
  110. {
  111. cout << "Patient ID: " << temp->ID << endl;
  112. cout << "Name : " << temp->name << endl;
  113. cout << "Age : " << temp->age << endl;
  114. cout << "Glucose : " << temp->glucose << "\n\n";
  115.  
  116. temp=temp->next;
  117. }
  118. }
  119.  
  120. // ---------- FUNCTION ListMax -------------------------
  121. void List::ListMax()
  122. {
  123. recordptr temp = head;
  124. int max=0;
  125.  
  126. if(temp!=NULL)
  127. max = temp->age;
  128. temp = temp->next;
  129.  
  130. while (temp!=NULL)
  131. {
  132. max = (max > temp->age)?max:temp->age;
  133. temp = temp->next;
  134. }
  135.  
  136. cout << "Highest age: " << max << "\n";
  137. // while (temp->age==max) I have tried adding this but it only
  138. // gives me errors when I run the program
  139. // cout << "The age is : " << temp->age;
  140. }

and in main:

  1. int main()
  2. {
  3. List l;
  4. for(int i=0; i<3; i++)
  5. l.ListInsert(i);
  6.  
  7. l.PrintList();
  8. l.ListMax(); // invoke to check for minimum value for first print of list
  9. return 0;
  10. }


Thanks for your help,
Gro
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Looking up and displaying values in linked lists

 
0
  #2
Mar 31st, 2006
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.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Looking up and displaying values in linked lists

 
0
  #3
Mar 31st, 2006
What would happen if you ran this code?
  1. void List::ListMax()
  2. {
  3. recordptr temp = head;
  4. recordptr max = head;
  5. int maxAge = 0;
  6.  
  7. if(temp!=NULL)
  8. maxAge = temp->age;
  9.  
  10. temp = temp->next;
  11.  
  12. while (temp!=NULL)
  13. {
  14. if ( temp->age > maxAge)
  15. {
  16. maxAge = temp->age;
  17. max = temp;
  18. }
  19. temp = temp->next;
  20. }
  21. if ( max != NULL )
  22. cout << max->age;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 5
Reputation: gropedersen is an unknown quantity at this point 
Solved Threads: 0
gropedersen gropedersen is offline Offline
Newbie Poster

Re: Looking up and displaying values in linked lists

 
0
  #4
Mar 31st, 2006
Thanks for your very rapid answers! I will try to implement it in the code and make it work.

Thank you
Gro
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC