【问题标题】:Can't figure out the meaning of this recursive code无法弄清楚这个递归代码的含义
【发布时间】:2012-04-12 19:14:44
【问题描述】:

我不明白这个递归代码的含义。它是否检查连续两个节点中的任何两个值是否相同?

bool has_repeats(element_t *e) 
{
    if (e == NULL) 
       return false;
    if (contains_value(e->next, e->val)) 
       return true;
    return has_repeats(e->next);
}

提前感谢您的帮助!

【问题讨论】:

    标签: c recursion


    【解决方案1】:

    是的,您通过回答自己的问题就知道您在说什么!用下一个节点检查当前节点。如果它们相同,则返回true,否则递归调用自身return has_repeats(e->next);

    我想这将有助于我们了解contains_value(x, y) 的实际作用。正如@Ben 评论的那样,contains_value(x,y) 很可能会检查列表中的所有值。

    【讨论】:

    • 我怀疑contains_value() 不仅检查下一个值,而且检查列表中的所有值。在这种情况下,该函数会检查重复项,而不仅仅是连续重复项(考虑到它的名称,这很有意义)
    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多