# Thread: can figure out what this function does..

1. ## can figure out what this function does..

its so complicated
i cant see the general way
Code:
Node *temp, *prev, *next, *prevmax, *newhead;
prevmax = prev = next = NULL;
printf("%d ", temp->value);
next = temp -> next;
if((!prev || prev->value < temp->value) &&
(!next || temp->value > next->value)){
if(! prevmax )
else
prevmax->next= temp;
prevmax = temp;
}
if(prev && prev!=prevmax)
free(prev);
prev = temp;
}
prevmax -> next = NULL;
putchar('\n');
}
}

2. It would appear to find the largest element in a singly-linked-link, and free the entire list except for that element. I would also add that this code smells, and I can't tell you for certain that it is bug free/works. If it does indeed free everything but the largest element, then I would have written it:
Code:
{
Node *largest = NULL, *i, *temp;

// Find the max
{
largest = (largest && largest->value > i->value ? largest : i);
}

// Free everything but the max
{
temp = i;
i = i->next;
if(temp != largest)
{
free(temp);
}
}

return largest;
}
About 15 lines less, much easier to read, and I can even tell you that it runs in O(N).