0

I have tried to convert this method that is already working in my program to recursive way. Because I was asked to do it. The thing is I have tried see below but in my method when try to add to he position the method value this value is a great number and create segmentation.

This is my method in iterative way:

int researchList_getPosByCountry(tResearchList* list, tCountry *country) {

// Check preconditions
    assert(list != NULL);

    tResearchListNode *prev = NULL;
    int pos;


    // check if is an empty list 
    if (researchList_empty(list)) {
        pos = -1;
    }
    else{

        pos = 1;
        prev = list->first;
        while ((prev != NULL) && !country_equal(prev->e->country, country) ) {
            prev = prev->next;
            pos++;
        }
    }

    if (prev == NULL) {
        pos = -1;        
    }

    return pos;
}

This is my method in recursive way:

 assert(list != NULL);

    tResearchListNode *prev;
    int pos;


    // check if is an empty list 
    if (researchList_empty(list)) {
        pos = -1;
    }
    else{
        pos = 1;
        prev = list->first;
        if ((prev != NULL) && !country_equal(prev->e->country, country) ) {

            prev = prev->next;
          pos = pos + researchList_getPosByCountry(list, country); //Debugging the segmentation is here
        }


    }
1
  • Apparently you have two structs, one for the list and one for the nodes. The recursive function must recurse on the nodes, not the list: You have three cases: (1) the node is null: return "not found"; (2) the node has the data you're lloking for: return the posuition; (3) recurse to node->next. I think your implementation recursively call the function on the list and always starts at the beginning. Commented May 4, 2020 at 7:14

1 Answer 1

1

You will get an endless recursion, since you call researchList_getPosByCountry always starting from the begin of the list; again and again and ...

I suggest you introduce a second (then recursively used) function that calls itself with the respective next node and returns (a) the "greatest" negative number possible if the node was not found (thereby turning any intermediate results on the call stack into a negative one), or return 0 to denote "country found, take the count so far", or "1 + next try" to continue counting. The recursive part could look as follows; you need then to call this one from your int researchList_getPosByCountry(tResearchList* list, tCountry *country) and interpret a negative result accordingly:

int researchList_getPosByCountry(tResearchListNode* node, tCountry *country) {
    if (!node) {
       return INT_MIN;
    } else if (countryEqual(...)) {
       return 0;
    } else {
       return 1 + researchList_getPosByCountry(node->next,country);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, as you mention the only way I founded to solve it is using an aux recursive function with two index. Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.