Recent

Author Topic: No recursive procedure - sorted by file size  (Read 2624 times)

pusuni

  • Jr. Member
  • **
  • Posts: 72
No recursive procedure - sorted by file size
« on: September 20, 2015, 11:54:01 am »
Hi! I have the following code. The code works properly

Code: [Select]

type
   { Mis tipos de Datos }
   tFichero = record
      Nombre             : TFilename;  // Name
      Tamano             : Int64;      // size of file
   end;

   tListaFicheros = ^tNodoFichero;
   tNodoFichero = record
      contenido          : tFichero;
      siguiente          : tListaFicheros;
   end;


   
function CreaLaListaDeFicheros (dato: tFichero ) : tListaFicheros;
   var
      PunteroAuxiliar  : tListaFicheros;
   begin
      new (PunteroAuxiliar);
      PunteroAuxiliar^.contenido := dato;
      PunteroAuxiliar^.siguiente := NIL;
      CreaLaListaDeFicheros :=  PunteroAuxiliar;
   end;   
   
   
procedure InsertaUnElementoEnFicheros (var LaLista : tListaFicheros; dato : tFichero );
   var
      PunteroAuxiliar   : tListaFicheros;
   begin 
       if Lalista = NIL then LaLista := CreaLaListaDeFicheros(dato)
         else         
            if LaLista^.contenido.Tamano < dato.Tamano then InsertaUnElementoEnFicheros (LaLista^.siguiente, dato)
               else
                  begin
                     new (PunteroAuxiliar);
                     PunteroAuxiliar^.contenido := dato;
                     PunteroAuxiliar^.siguiente := LaLista;
                     LaLista := PunteroAuxiliar;
                  end; 
   end; 



The code creates a list sorted by file size, but the procedure InsertaUnElementoEnFicheros is a recursive procedure. I would like to do the same with that procedure but without any recursive.

I tried several times but I'm not able to write correct code without recursion.

H.E.L.P !!

Roland57

  • Hero Member
  • *****
  • Posts: 529
    • msegui.net
Re: No recursive procedure - sorted by file size
« Reply #1 on: September 20, 2015, 12:09:56 pm »
Hello!

Interesting problem but could you post a whole program, so we could easily test modifications?
My projects are on Codeberg.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: No recursive procedure - sorted by file size
« Reply #2 on: September 20, 2015, 12:58:36 pm »
I tried several times but I'm not able to write correct code without recursion.
Show this "several times" attempt

pusuni

  • Jr. Member
  • **
  • Posts: 72
Re: No recursive procedure - sorted by file size
« Reply #3 on: September 21, 2015, 01:15:00 pm »
Finally, I got it!!

Code: [Select]
procedure InsertaUnElementoEnFicheros (var LaLista : tListaFicheros; dato : tFichero );
   var
      PunteroAuxiliar, PunteroPrecedente, PunteroTMP   : tListaFicheros;
      Insertado         : BOOLEAN;
   begin
      if Lalista = NIL then LaLista := CreaLaListaDeFicheros(dato)
         else
            begin
               PunteroAuxiliar := Lalista;
               PunteroPrecedente := LaLista;
               Insertado := FALSE;
               while (PunteroAuxiliar <> NIL) and (not Insertado) do
                  begin
                     if dato.tamano <= PunteroAuxiliar^.contenido.tamano then
                        begin
                           Insertado := TRUE;
                           new (PunteroTMP);
                           PunteroTMP^.contenido := dato;
                           PunteroTMP^.siguiente := PunteroAuxiliar;
                           if PunteroPrecedente = LaLista then LaLista := PunteroTMP
                           else PunteroPrecedente^.siguiente := PunteroTMP;
                        end
                     else
                        begin
                           PunteroPrecedente := PunteroAuxiliar;
                           PunteroAuxiliar := PunteroAuxiliar^.siguiente;
                        end;
                  end;
               if not Insertado then
                  begin
                     new (PunteroTMP);
                     PunteroTMP^.contenido := dato;
                     PunteroTMP^.siguiente := NIL;
                     PunteroPrecedente^.siguiente := PunteroTMP;
                  end;
            end;       
    end;



Best Regards!!

 

TinyPortal © 2005-2018