You have misunderstood the "do nothing" part a bit.
The statements inside the for-loop are still executed.
Maybe it is easier if you look upon it this way:
for (cur = nodelist , follow = NULL; cur != NULL && cur->value != n; follow = cur , cur = cur->next)
{
}
[Edit] (Corrected the wording for the assignments)
So, as long as the pointer
cur
is not NULL and the value of
cur->value
is not equal to
n
,
follow
will be assigned the value of
cur
,
cur
will be assigned the value of
cur->next
and the loop continues.
When the loop exits, the variables are checked and actions are taken.
For this part, it is the cleanup and return statement of the method.
free(cur); return nodelist;
The thing is that you didn't copy the full method, so I don't know what else is going on.
cur
,
follow
and
n
are defined elsewhere.
[UPDATE] due to comment
I suppose you mean like this
if (follow == NULL)
{
nodelist = nodelist->next;
}
else
{
follow->next = cur->next;
free(cur);
return nodelist;
}
The first problem with this is that all branches of your code will not have a return statement. This can of course be fixed like this:
if (follow == NULL)
{
nodelist = nodelist->next;
return nodelist;
}
else
{
follow->next = cur->next;
free(cur);
return nodelist;
}
This will pass the compilation, but it will result in a memory leak as the variable cur is not cleared. Again you can fix this:
if (follow == NULL)
{
nodelist = nodelist->next;
free(cur);
return nodelist;
}
else
{
follow->next = cur->next;
free(cur);
return nodelist;
}
This will work but you have added the same code twice, which is always a bad idea.
So the good practice is:
if (follow == NULL)
nodelist = nodelist->next;
else
follow->next = cur->next;
free(cur);
return nodelist;
So now we are back where we started.
One issue is that you only copied part of the code.
It should be more like:
List* FooBar(List* nodelist, int n)
{
List* cur = NULL;
for(cur = nodelist, follow = NULL; cur != NULL && cur->value != n; follow = cur, cur = cur->next) ;
if (cur == NULL)
{
return nodelist;
}
if (follow == NULL)
{
nodelist = nodelist->next;
}
else
{
follow->next = cur->next;
}
free(cur);
return nodelist;
}
(List* is a pointer to an imaginary type called List)
You really should read up on for-loops to get a better understanding of the conditions and statements with in the loop declaration.
C Programming Loops[
^]