0
struct Node *xFromEnd(struct Node *pHead, int x)
{
    static int temp = 1;

    if (pHead->next != NULL)
        xFromEnd(pHead->next, x);
    if ((temp++) == x)
        return pHead;
}

How do I break out of the function when the condition is met? The return is just going further up on the call stack(going to its previous function call) rather than exiting and going to main. How can I do that?

  • please see [this](http://stackoverflow.com/questions/25676961/understanding-how-recursive-functions-work) question first. – Parham Alvani Mar 12 '15 at 22:21
  • I understand that. Recursion is climbing up a ladder one step at a time and then climbing down one step at a time. What I'm asking is can I jump straight down once I've found my "step" like in this case? – Meith Jhaveri Mar 12 '15 at 22:26
  • If you want, you can use something like dynamic programming and then you can break when you find your answer. – Parham Alvani Mar 12 '15 at 22:29
  • `temp == x` for only one value of `temp`. That should give you a hint re: your ladder. – Michael Foukarakis Mar 12 '15 at 22:31
  • That's correct. And if that one value is found, is there a way I should be able to come straight out of the call stack? I know what I'm doing wrong here, it's just that I want to know what I'm imagining is possible or not at all in c. – Meith Jhaveri Mar 12 '15 at 22:33
  • You might want to start with adding an explanation of what you are trying to achieve in the first place. – dhke Mar 12 '15 at 22:35
  • There is, but first you should understand what your code _actually_ does. Step through it, or insert some logging statements, or follow it on paper. When `temp >= x`, your bug will become apparent. – Michael Foukarakis Mar 12 '15 at 22:36
  • I used breakpoint to step through the process. I go till the end of the list(say 10 steps up the steps till it reaches NULL) in the first if condition statement. Then say I want the 3rd value from the end(x=3). So i step through it, and when the condition is met(temp == x), I have found my value and want to return. So the line: return pHead; is executed. However, this return instead of going to main, just goes on to the next fucntion call. Which i now understand is correct behavior and it will do so till all steps(in our case 10) are completed. Can I come straight out to the main? If yes how – Meith Jhaveri Mar 12 '15 at 22:41
  • @ dhke - I'm trying to find the xth number from back of the link list. Data available to me is head of the list and x. – Meith Jhaveri Mar 12 '15 at 22:51

2 Answers2

0
struct Node *xFromEnd(struct Node *pHead, int x){
    static int temp = 1;//but can not reset !!

    if(pHead == NULL)
        return NULL;
    struct Node *p = xFromEnd(pHead->next, x);
    if (p == NULL){
        return (x == temp++) ? pHead : NULL;
    }
    return p;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thank you for sharing a solution, however I see that 'p' is getting returned unnecessarily even after x==temp condition is met where pHead is returned. I would like to find a way to avoid this. Hence my question is, is it possible to break out straight to main once pHead is returned? – Meith Jhaveri Mar 12 '15 at 23:07
  • In the case of C does not allow you to straight return to main. – BLUEPIXY Mar 12 '15 at 23:12
  • There is a need for global escape from the recursive call.(c++ have try..catch, but...) – BLUEPIXY Mar 12 '15 at 23:17
  • or use `longjmp`...they deviate from the spirit that returns a value. – BLUEPIXY Mar 12 '15 at 23:28
-2

I think than your function never break because in your function you have an increment of temp variable which return true value, and not the value of temp variable.

Try :

temp++;
if (temp == x)
return pHead;
Christopher
  • 136
  • 9