Checking for a specific string inside of a linked list?

I'm currently trying to go through a linked list in my program and see if it contains a few specific strings. However, right now, it's not detecting any of the matching strings, even though I know they're in there. When I print out my Tree and the Linked List inside, they show up. For some reason, this function isn't catching them however.

I'll try to keep it brief, as there's a lot of code here, but let me know if you need to see anything else. Thanks!

Code for passing the actorName for checking:

1
2
3
4
5
6
7
8
9
10
11
  void BinarySearchTree::PrintShowsByActor_InOrder(BSTNodePtr node) {
	if (node != NULL) {
		PrintShowsByActor_InOrder(node->left);
		bool isTrue = node->ShowInTree->Actors->HasThisActor("TimConway");
		if (node->ShowInTree->Actors->HasThisActor("Tim Conway") == true) {
			cout << "Tim Conway found." << endl;
			cout << node->ShowInTree->showName << endl;
		}
		PrintShowsByActor_InOrder(node->right);
	}
}



Code for actually performing the check:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool LinkedList::HasThisActor(string passedActorName) {
	curr = head;

	while (curr != NULL) {

		if (curr->actorName == passedActorName) {
			return true;
			break;
		}

		else {
			curr->next;
			return false;
		}
	}
}
Why don't you test by printing out the value of curr->actorName and passedActorName inside the HasThisActor method? See if it shows up.
Last edited on
TheToaster, I just tried your suggestion. The strange thing was, I don't think it's actually getting through the Linked List at all, which is strange. It's only pulling the first actor of every show in the tree and then immediately exits the while loop. I tried removing the return false; bit of the else statement, but then I got an infinite loop on the very first actor in the tree. It never moved on to the next node. I may be missing something here in my code. What might be causing it to not move through the Linked List? I have it set up the exact same way my PrintList function is set up, and PrintList is working great...I appreciate the second pair of eyes, so thank you.
It looks like you've declared curr as a member variable. That is incorrect. It should just be local to the function(s) that use it.

There's no need to pass a copy of the string when a const reference will do.

Generally it's considered a bad idea to typedef away a pointer. So BSTNode* is better than BSTNodePtr.

As for what's causing the errors you've noticed, you should only return false after the loop. And you aren't setting curr to curr->next. You're just saying curr->next (which does nothing).

1
2
3
4
5
6
bool LinkedList::HasThisActor(const string& passedActorName) {
	for (BSTNode* curr = head; curr; curr = curr->next)
		if (curr->actorName == passedActorName)
			return true;
	return false;
}

Dutch, you were right. It was as simple as changing it to an address of and a "const" in the function definition. And changing it to actually loop through helped as well. I knew I was missing something simple there, so thank you.

What makes the & operator required there in the definition? I thought that that was for address of, so wouldn't that just make it take in the address of the string instead of the actual string, breaking the equals?
The & is not the address-of operator but the reference operator in this case. It causes a "reference" to be passed instead of a copy. You should have been taught about references (they are kind of important!).

But changing it to a reference is not actually needed in order for it to work properly. It's just more efficient since it doesn't create a copy of the string.

What fixed the basic problem was moving the return false to after the loop and actually assigning curr->next to curr.
Ah okay. Unfortunately, my "intro" class was very very bad. We got to structs in the very last week, took the final, and the other section for the course had already got to classes and pointers. Those were on the final, and my section got destroyed. So I went into Data Structures knowing nothing about pointers and classes, and I've found that pointers especially are a quite difficult concept to grasp, even with all the resources of the internet. I know what they do, but aside from lists, I don't understand the practicality. But I really appreciate the help. It should definitely keep me moving forward and learning!
Topic archived. No new replies allowed.