943,919 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2231
  • C++ RSS
Mar 31st, 2006
0

Looking up and displaying values in linked lists

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gropedersen is offline Offline
5 posts
since Oct 2005
Mar 31st, 2006
0

Re: Looking up and displaying values in linked lists

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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Mar 31st, 2006
0

Re: Looking up and displaying values in linked lists

What would happen if you ran this code?
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 31st, 2006
0

Re: Looking up and displaying values in linked lists

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

Thank you
Gro
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gropedersen is offline Offline
5 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: program help
Next Thread in C++ Forum Timeline: Binary Search Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC