String linked list trouble

I am trying to call my function list.getdata(10) which should return the tenth position in my linked list which has the word "what" it does but when I return this as a string and call my list.isInList2(temp) it completely skips the function call. But If I call list.isInList2(temp) when I set temp="what" instead of temp=list.getdata(10) it works fine any suggestions? Thank you!
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
34
35
36
37
38
39
string temp;
temp=list.getdata(10); //calls get data...this should return the word "what"
//temp="what";
cout<<"In temp spot:"<<temp<<endl;// does return the word "what"
list.isInList2(temp); //should show 10 comparisons.....does not says 1
cout<<"Took "<<compair<<" comapirisons"<<endl;
cout<<"Total words in the linked list is: "<<count<<endl;


//my gatdata function
string IntSLList::getdata(int pos)
{
	string it;
	for(int i=1; i<=pos; i++)
	{
		
	if(i==pos)
	head -> getit();

	else
	head= head->getnext();
	}
	it=head->getit();
	//cout<<head->getit()<<endl; //test to see what the word was displays "at" when called with the int pos 2
	return it;
}


//my isInList2 function
bool IntSLList::isInList2(string el) const {
    IntSLLNode *tmp;
    for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next)
	{
		cout<<"value in tmp in the isinList function: "<<tmp->info<<endl;
		compair++;
	}
    return tmp != 0;
}
Last edited on
17
18
19
20
21
if(i==pos)
    head -> getit();

else
    head= head->getnext();

On line 21 here, you're changing where the head of the list is.

This would have been easier to spot if you had made getdata a const function (presumably, a getdata function shouldn't change the list):
string IntSLList::getdata(int pos) const
That makes sense, ok so now changing that I had to make a pointer to the head to step through my function, when I did that it only goes through 2 times and get stuck at position 2 in the linked list but it does now properly display 2 comparisons for the word "am" when called using temp=list.getdata(10) tho position 10 is still "what".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

string IntSLList::getdata(int pos) const
{
	IntSLLNode *temper=head;
	string it;
		for(int i=1; i<=pos; i++)
		{
		
		if(i==pos)
		temper -> getit();

		else
		temper= head->getnext();
		}
		it=temper->getit();
		cout<<"This is in it: "<<it<<endl;
		//cout<<head->getit()<<endl; //test to see what the word was displays "at" when called with the int pos 2
		return it;
}
temper= head->getnext();
should be
temper= temper->getnext();
(see why?).
Ahh yes cuz temper needs to transverse the linked list not head head can only move one spot, correct?
Well, to be more precise, head will always be in the same spot, so head->getnext() will always be pointing to the same element (the second one, presumably).

So yeah, basically.
Ahh ok, Thank you so much for your help! Linked list is a new topic in class, thank you again!
Topic archived. No new replies allowed.