doubt recursion

0

I have a sample code of a printing procedure recursively in reverse order of the contents of a list that I do not understand. This is the code:

My doubt: I imagined the operation of this, I thought it would be like this: start the execution on the first node, we reach if (n != NULL) , effectively, it is not NULL, we reach the recursive call, we return to start but in the next node and so on until the last node, when we get to the last node what happens in the recursive call? because n->get_next() is NULL . An explanation of how the recursive call is undone

Should the recursive call not be write_reverse1?

template <class T>
ostream& dll_t<T>::write_reverse1(dll_node_t<T>* n, ostream& os) const {

        if (n != NULL)
            reverse1(n->get_next(), os);
        else
            n->write(os);

        return os;
    }

this second method what is it for?

    template <class T>
    ostream& dll_t<T>::write_reverse1(ostream& os) const {

        reverse1(head_, os);

        return os;
    }

thank you very much

    
asked by AER 25.07.2017 в 19:33
source

1 answer

1

in pseudocodigo .. (I leave the implementation to you)

imprimir-reversa(nodo n, lista l)
{
    si n != null
    {
      imprimir-reversa(l.next-nodo, l)
    }
    imprimirnodo(n)
}

Therefore, yes, the recursive call would have to be write_reverse1.

    
answered by 25.07.2017 / 22:42
source