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

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.

Jump to Post

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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.