How to search a linked list for data and return the data.
I know I am not supposed to post my homework on here but I asked my teacher for help but she won't answer me. 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 student'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 structure I have a data member . In my linked list program, I call the function "retrieve" that is supposed to return the node's data I am searching for using the parameter.
This is my findStudent function:
Code:
void findStudent(const LinkedList<Student> &list)
{
int id;
cout << "Please enter student ID number: ";
cin >> id;
Student temp{ id }; //temporary struct object
if (list.retrieve(temp) == false) //can't find the student id i am searching for
cout << "Student not found.\n";
else
cout << temp;
}
And this is the retrieve function I call above:
Code:
template <class TYPE>
bool LinkedList<TYPE>::retrieve(TYPE &dataIn) const
{
bool success = false;
Node<TYPE> *pTemp = front;
while (pTemp != nullptr && pTemp->data < dataIn)
{
pTemp = pTemp->next;
}
if (pTemp != nullptr && pTemp->data == dataIn)
{
success = true;
dataIn = pTemp->data;
}
return success;
}
How can I get retrieve to find the student I am searching for?