Pascal/Turbo Pascal - Como hago recursiva esta funcion?

 
Vista:

Como hago recursiva esta funcion?

Publicado por Agustin Guerra (5 intervenciones) el 17/11/2015 00:41:12
Hola, tengo la siguiente funcion que cuenta los elementos de una lista y quiero hacerla recursiva. Alguna sugerencia? Gracias

1
2
3
4
5
6
7
8
9
10
11
function long_lista(l:lista):integer;
 var
  num:integer;
  begin
       num:=0;
       while (l <> nil) do begin
         num:= num +1;
         l:=l^.sig
       end;
      long_lista:=num;
  end;
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Como hago recursiva esta funcion?

Publicado por ramon (2158 intervenciones) el 21/11/2015 10:57:15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{Mira esto}
 
 program long_lista;
   uses
      crt;
   type
     lista = ^lnodos;
     lnodos = record
            numero : integer;
               sig : lista;
             end;
 
    var
      prim, anter, actu : lista;
      n, dat : integer;
 
 
    procedure crealista(d : integer);
    var
      pp, da : integer;
    begin
        for da := 1 to d do
        begin
          pp := random(d + d) + 1;
          if prim = nil then
          begin
             new(actu);
             actu^.numero := pp;
             prim := actu;
             actu^.sig := nil;
          end
       else
          begin
             anter := actu;
             new(actu);
             actu^.numero := pp;
             anter^.sig := actu;
             actu^.sig := nil;
          end;
       end;
    end;
 
    function lon_lista(l : lista) : integer;
    begin
       if l = nil then
       lon_lista := n
     else
       begin
        n := n + 1;
        lon_lista := lon_lista(l^.sig);
        end;
    end;
 
    procedure presenta_numeros;
    var
      temp : lista;
    begin
       temp := prim;
       while temp <> nil do
       begin
         writeln('   ',temp^.numero);
         temp := temp^.sig;
       end;
    end;
 
 
 
    begin
       clrscr;
       prim := nil;
       n := 0;
       crealista(10);
       writeln;
       writeln('   Longitud Lista =  ',lon_lista(prim));
       writeln;
       presenta_numeros;
       readkey;
    end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

Como hago recursiva esta funcion?

Publicado por Agustin Guerra (5 intervenciones) el 21/11/2015 12:38:02
Exacto! Muchisimas grcias. Era asi de simple, pasando un n=0 por referencia.
Se agradece!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar