Linked list recursion..Why do I need return statement?

It always returns the value it stores into *node, and in your modified code it loses that value since the recursive call passes a local temp rather than the place it actually needs to put the value, and then does the store after it returns. A very odd construct. You can make addRecursion void (and simpler) if you just get rid of that local var: void addRecursion(lnode **node, lnode *newNode){ if(*node == NULL){ *node = newNode; }else{ addRecursion(&(*node)->next, newNode); } }.

To assign something to (*node)->next. That points to the next node of the list, I suppose. So without that assignment, the list does not go to the last node, to which the new node is to be added.

Recursion could be replaced with iteration.

Because the function is returning the address of the next node which is required in your algorithm to set the final node.

It might help to visualize the call stack growing/shrinking as the recursion progresses. Assume the following list: node1 -> node2 -> node3 -> null Then addRecursion unfolds as follows (psuedocode): 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 The new node is added to the end and each link in the chain is preserved.

It should also work if you make this adjustment to your code where you re-assign the value of nextNode back to (*node)->next since you passed that value by reference to the recursive function call (and therefore it was modified during the later recursive calls): 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 version above is a bit cleaner though since it axes out two assignments and in doing so also saves some space on the stack since there is no storage required for the local pointer variable nextNode.

It always returns the value it stores into *node, and in your modified code it loses that value since the recursive call passes a local temp rather than the place it actually needs to put the value, and then does the store after it returns. A very odd construct. To assign something to (*node)->next.

That points to the next node of the list, I suppose. So without that assignment, the list does not go to the last node, to which the new node is to be added. Recursion could be replaced with iteration.

Because the function is returning the address of the next node which is required in your algorithm to set the final node. It might help to visualize the call stack growing/shrinking as the recursion progresses. The new node is added to the end and each link in the chain is preserved.

Chris's version above is a bit cleaner though since it axes out two assignments and in doing so also saves some space on the stack since there is no storage required for the local pointer variable nextNode.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions