How to search a linked list for certain data


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;
}

And this is the retrieve function I call above:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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?

Last edited on
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
I can't get my function to find the student.


 
while (pTemp != nullptr && pTemp->data < dataIn)


Is your list sorted?

Is the operator < implemented properly for Student?
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
template <class TYPE>
TYPE *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)
	{
		return &pTemp->data;
	}

	return nullptr;
}

void findStudent(const LinkedList<Student> &list)
{
	int id;		
	
	cout << "Please enter student ID number: ";
	cin >> id;

	Student temp{ id };
	Student *found = list.retrieve(temp);

	if (found == nullptr)	//can't find the student id i am search ing for
		cout << "Student not found.\n";
	else
		cout << *found << '\n';
}
@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)

however, I do find your interface more intuitive.
Topic archived. No new replies allowed.