0

I have a problem with this code too. right now I have a segmentation fault but before I couldn't get the correct maxValue.

int maxValue(node* head, int max)
{
    node* p;
    
    if (head == NULL)
    {
        return(1);
    }
    else
    {
           
        p = head;
        max = p-> item;
        while(p != NULL)
        {
            p = p-> next;
            if (p->item > max)
            {
                max = p-> item;
            }
            p = p-> next;
            

        }
        return max;
    }
    
}
2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by iacosar
0

You have this line twice

p = p-> next;

I think you want it only once at the end of the loop so the loop condition catches it if its null.

0

tks, I fix my problem. The main error was on the main, I wasn't storing the returned value anywhere.

I used

max = maxvalue( my head, int x). where x is the returning value.

cheers

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.