-1
def imprimeDeTrasParaFrente(lista):
if lista == None :
return
imprimeDeTrasParaFrente(lista.proximo)
print lista,
class Node(object):
def __init__(self, carga=None, proximo=None):
self.carga = carga
self.proximo = proximo
def __str__(self):
return str(self.carga)
no1, no2, no3 = Node(1), Node(2), Node(3)
no1.proximo, no2.proximo = no2, no3
imprimeDeTrasParaFrente(no1)
With the function printDeTrasParaFrente() I was able to print the list elements in reverse order, and it only checks whether the list is empty and calls itself with the next node in the list. In my perspective the print function is never called because when the list is empty the function simply returns and ends the program, or I’m wrong?
I was able to understand the logic now, recursive functions call themselves within the function, the Return command is terminating the function within the function’s own scope that means the program flow goes back to the scope of the main function so it would be possible to add one more Return to the end of the print function so recursive functions can contain two Return but at the end return only one.
– jiraya