0

i'd like to share a problem i'm struggling with. I need to create two recursive function (with tail recursion, i'm not allowed to put multiple returns inside the body of the function) and the functions are: Calculate the intersection between two sets of numbers. Calculate the symmetric difference between two sets of number.

I've already done the function that calculate the intersection, and i will post it in the code section. How can i calculate the symmetric difference in the way i calculated the intersection? Because the symmetric difference is the opposite of the intersection, and maybe i can reuse some of the code i already written for the symmetric difference function.

Note: the two sets of numbers are saved in two linked lists and they are already sorted, and every number in a set is unique, they can't repeat.

Here is the function for the intersection (it works well with all the case):

t_nodo *intersezione_insieme(t_nodo *insieme_A, t_nodo* insieme_B)
{ 
    t_nodo *tmp;
    if (insieme_A == NULL || insieme_B == NULL) {
        tmp = NULL; 
    }
    else
    {
        while (insieme_A->info < insieme_B->info && insieme_A->succ != NULL) {
            insieme_A = insieme_A->succ;
        }
        while (insieme_A->info > insieme_B->info && insieme_B->succ != NULL) {
        insieme_B = insieme_B->succ;
        }

        tmp = (t_nodo*)malloc(sizeof(t_nodo));

        if (insieme_A->info == insieme_B->info) {
            tmp->info = insieme_A->info;   
            tmp->succ = intersezione_insieme(insieme_A->succ, insieme_B->succ);
        }
        else
        {
            tmp = intersezione_insieme(insieme_A->succ, insieme_B->succ);
        }
    }
    return tmp; 
} 
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105

0 Answers0