| | |
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:
Solved Threads: 0
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.
and in main:
Thanks for your help,
Gro
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)
#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:
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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.
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."
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
What would happen if you ran this code?
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- Stacks using doubly linked lists (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: program help
- Next Thread: Binary Search Tree
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






