15.1-1
Show how to modify the PRINT-STATIONS procedure to print out the stations in increasing order of station number. (Hint: Use recursion.)
Resolução
PRINT-STATIONS(l, n, i)
1 if n ≥ 2
2 PRINT_STATIONS(l, n - 1, li[n])
3 print "line " i ", station " n
A chamada a PRINT-STATIONS(l, n, l*), tal que n é o último elemento de l e l* é a última linha utilizada, retorna a saída do problema em ordem crescente de número de estação. No exemplo da Figura 15.2, PRINT-STATIONS(l, 6, 1) produzirá a saída:
line 1, station 1
line 2, station 2
line 1, station 3
line 2, station 4
line 2, station 5
line 1, station 6