English 中文(简体)
相关名单重新编号。 为什么我需要返回的话?
原标题:Linked list recursion..Why do I need return statement?
void add(llist *list, lnode *newNode){
    list->size++;
    addRecursion(&list->head, newNode);
}

lnode* addRecursion(lnode **node, lnode *newNode){
    if(*node == NULL){
        *node = newNode;
    }
    else{
        lnode *nextNode = (*node)->next;
        (*node)->next = addRecursion(&nextNode, newNode);

    }
    return *node;
}

This code works fine.. I took a look at the codes online and made a few changes. But I still don t understand that why addRecursion function has to have the return type. I changed the function like

void addRecursion(lnode **node, lnode *newNode){
        if(*node == NULL){
            *node = newNode;
        }
        else{
            lnode *nextNode = (*node)->next;
            addRecursion(&nextNode, newNode);

        }
    }

然后,它就没有工作。

最佳回答

它总是将它所储存的价值退回到*node,在经过修改的法典中,它失去这一价值,因为休养式电话带走了当地节奏,而不是它实际需要的位置,然后是该仓库返回之后。 一个非常奇怪的构造。 如果你刚刚摆脱这一地方的var,你就能够增加复职(更简单):

void addRecursion(lnode **node, lnode *newNode){
    if(*node == NULL){
        *node = newNode;
    }else{
        addRecursion(&(*node)->next, newNode);
    }
}
问题回答

分配到<代码>(*node)->next。 这表明名单上的下一个名字,我说了。 因此,如果没有这一分配,清单就没有到最后的节点,而要增加新的节点。 可以代之以回收。

由于这项职能正在回到你算法中为确定最后定点所需的下游的地址。

随着再入侵的进展,它可能会帮助视线增加/缩小。 名单如下:

node1 -> node2 -> node3 -> null

接着<代码>Recursion发展如下(opuedocode):

addRecursion(&node1, newNode)
    node1.next = addRecursion(&node2, newNode);
        node2.next = addRecursion(&node3, newNode);
            node3.next = addRecursion(null, newNode); // returns &newNode
            node3.next = &newNode;
        node2.next = &node3;
    node1.next = &node2;
// return value ignored

新节添加到尾端,链条的每一环节都得到维护。

如果你在重新指定<代码>nextNode 重至(*node)->next的价值,因为你参照休养职能呼吁,将这一价值转过来,(因此在后来的休养电话中作了修改):

void addRecursion(lnode **node, lnode *newNode)
{
    if(*node == NULL)
    {
        *node = newNode;
        return;
    }

    lnode *nextNode = (*node)->next;
    addRecursion(&nextNode, newNode);
    (*node)->next = nextNode; //Add this line to re-connect the link
}

上面的Chris s版本是比较清洁的,因为它列出了两个任务,因此也节省了部分空间,因为当地点变量nextde不需要储存。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签