for循环,然后是分号,然后是if / else语句? [英] For loop, then semicolon at the end, and then if/else statements?

查看:82
本文介绍了for循环,然后是分号,然后是if / else语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当分号在for语句的末尾时,它意味着没有。但是这个怎么样:



I know it means to do 'nothing' when a semicolon is at the end of a for statement. But what about this:

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





我很困惑。此代码用于从链接列表中删除节点,一切正常但我不明白此代码中发生了什么。怎么能循环然后什么都不做?然后它与if / else语句进行比较?除此之外



Im very confused. This code is for deleting nodes from a linked list, everything works fine but i do not understand what is going on in this code. How can it loop and then do nothing? And then it compares with if/else statements? On top of that

free(cur);
return nodelist;



这2行不属于for循环的if / else声明。这一切都有效吗?它每次循环都比较?如果这是为什么当我将整个代码嵌入大括号时我的代码崩溃了?我需要一些建议,谢谢!


These 2 lines do not belong in any if/else of for loop statement. How is this all working? Every time it loops is it comparing? If it is why is my code crashing when i nest this whole code in braces? I need some advice, thank you!

推荐答案

你误解了什么都不做的部分。

里面的陈述-loop仍在执行。



如果你这样看也许会更容易:

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)
{
    // For each iteration do nothing
}





(更正了作业的措辞)

所以,只要指针 cur 不为NULL且 cur->值的值不等于 n 关注将被赋予 cur 的值cur 将被赋值为 cur-> next 并继续循环。



当循环前它,检查变量并采取措施。



对于这部分,它是方法的清理和返回声明。



(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);  // This frees the memory allocated 
return nodelist;



问题是你没有复制完整的方法,所以我不知道还发生了什么。

cur 关注 n 已定义其他。



[更新]由于评论

我想你的意思是这样的


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





第一个问题是代码的所有分支都没有return语句。这当然可以修改如下:



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



这将通过编译,但由于未清除变量cur,因此会导致内存泄漏。你可以再解决这个问题:


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 *是指向名为List的虚构类型的指针)





你真的应该阅读for循环以更好地理解循环声明中的条件和语句。

C编程循环 [ ^ ]


除了解决方案1:



虽然它显然有用,会考虑这种for-loop-use坏风格。我会改为写:



In addition to solution 1:

While it obviously works, I would consider this kind of for-loop-use bad style. I would write instead:

cur = nodelist;
follow = NULL;
while (cur != NULL && cur->value != n)
{
    follow = cur;
    cur = cur->next;
}


这是一段非常有趣的代码。

解释一下:

This is a very interesting piece of code.
lets explain it :
//cur is the currently selected node;
//follow is the mother of current node
 for(cur = nodelist, follow = NULL; /*here nodelist is the top. so nodelist has no mother. thus follow is null*/
cur != NULL && cur->value != n; /*Lets look at the condition. cur is null: here if cur is null then no need to continue. another condition is cur->value is not n; which means, the value we are looking for is found. if value==n which also means that cur is not null.*/
follow = cur, cur = cur->next/*final execution part; first follow getting the cur value; and cur value is getting the next node.(point: next node can be null.)*/
);//about semicolon; semicolon is the statement. in c/c++ an empty semicolon is also a statement.
 
    if (cur == NULL) //here look back at for loop. if cur is null which means nothing to continue; so for loop stops; which also mean the value never matched. this can also mean that nodelist is null.
    {
        return nodelist;
    }
 
    if (follow == NULL) //if the follow is null, this means there is only one node in the linked list; this is the assignment part of for loop. and the value is matched in the first node of the lilst; 
    {
        nodelist = nodelist->next; 
    }
    else //if follow is not null which means linked list has more than one node and the match appeared somewhere in the middle of the linked list. 
    {
        follow ->next = cur->next; // here follow(the mother) is holding the next node of current node. which also means current not is removed from the list. but not deleted from memory
    }
    
    free(cur); //deleting from memroy
    return nodelist;
}





这是链接列表中的删除程序。



this is a delete procedure from a linked list.


这篇关于for循环,然后是分号,然后是if / else语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆