Segmentation Fault

1
2
3
4
5
6
7
8
9
10
11
12
int Link :: find_r(node *pNode)
{
                node *cur = head;
                if(cur->next != NULL)
                        return find_r(cur->next);


                else
                        return cur->data;


}


Can someone please help me? Where is the segmentation fault here? I figure when the function is calling cur->next at some point as a parameter. its jumping past the linked list, but I can not figure out how to fix it.
closed account (D80DSL3A)
Segmentation fault? I see a different problem.

I think this would lead to infinite recursion (oh-THAT may cause a seg. fault).
Your function does not use the node* passed to it (pNode). It just keeps using head over and over and over...

Try this and see what happens:

1
2
3
4
5
6
7
8
9
int Link :: find_r(node *pNode)
{               
       if(pNode->next != NULL)
            return find_r(pNode->next);


       else
            return pNode->data;
}


I think you would pass head to the function when you call it. This provides it with the starting point in the list. eg. if myList is an instance of a list

 
int lastValue = myList.find_r( myList.head );// looks like the function finds the last data value 
hey thank you! that worked!
Topic archived. No new replies allowed.