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

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.