For this program I am not allowed to use vectors or std::
I have a function called findStudent that takes a linked list structure as an argument. The function asks the user for a stundent's id number. I build a temporary structure object and search the linked list by calling the "retrieve" function. If the student is found them I am supposed to display the student's entire record. However, I can't get my function to find the student.
I use a structure named Student that stores all the student's info, and a linked list where each student's info is a node. In my linked list program, I cal the function "retrieve" that is supposed to return the node's data I am searching for using the parameter.
This is my findStudent function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void findStudent(const LinkedList<Student> &list)
{
int id;
cout << "Please enter student ID number: ";
cin >> id;
Student temp{ id };
if (list.retrieve(temp) == false) //can't find the student id i am search ing for
cout << "Student not found.\n";
else
cout << temp;
}
you may made a lot of mistakes in your custom list implementation
you may made a lot of mistakes in your usage of the custom list
you may made a lot of mistakes in your student class implementation
don't wanna guess, post enough code to reproduce your issue
`retrieve()' looks ok at a tired first glance for a sorted list
In findStudent() at line 13, you're printing the temp Student object that contains only the student ID. You want to print the Student object in the list that contains all the student info.
The problem is that the way the code is written now, you don't have access to that object. Your retrieve() method doesn't actually retrieve the object, it just says "yup, I have that student in the list."
Change retrieve() to return a pointer to the found Student, or NULL if the student isn't found. Then change findStudent accordingly:
@dhayden bool LinkedList<TYPE>::retrieve(TYPE &dataIn) const
note that the parameter is a non-const reference, that object is going to be modified
which happens on line 14 dataIn = pTemp->data;
so the function does provide access to the object on the list (it may not be implemented correctly as it needs operator== to only check the id)