Double List in Pascal

0

I'm doing a list exercise. The exercise asks me to create a double list of integers. I'm struggling to do it and I think I did it wrong. I read the integers of a file putting together a common list and then copy the list backwards ... Let's see if someone can give me an opinion.

Program G4E10;
Uses Crt;
Type
    ListEnt = ^TipoEntero;
    TipoEntero = record
        ant : ListEnt;
        int : Integer;
        sig : ListEnt;
        end;
    T_Lista = record
        Cbeza : ListEnt;
        Cola : ListEnt;
    end;
    Procedure Crear(var Lista: T_Lista);
        var Arch: Text; NuevoDato, Ant,Act: ListEnt;
        begin
        Lista.Cbeza := nil;
        Lista.Cola := nil;
        Assign(Arch, 'ENTEROS.txt');
        Reset(Arch);
        while not Eof(Arch) do 
            begin
            Ant := nil;
            Act := Lista.Cbeza;
            New(NuevoDato);
            Readln(Arch, NuevoDato^.int);
            while (Act <> nil) AND (Act^.int < NuevoDato^.int) do
                begin
                Ant := Act;
                Act := Act^.sig;
                end;
            if (Act = Lista.Cbeza) then
                begin
                NuevoDato^.sig := Act;
                NuevoDato^.ant := nil;
                Lista.Cbeza := NuevoDato;
                end
            else    
                begin
                NuevoDato^.ant := Ant;
                NuevoDato^.sig := Act;
                Ant^.sig := NuevoDato;
                end;
            end;
        Close(Arch);
        end;
    Procedure MostrarDer(Lista: T_Lista);
    begin
        while (Lista.Cbeza <> nil) do
            begin
            Writeln(Lista.Cbeza^.int, ' ');
            Lista.Cbeza := Lista.Cbeza^.sig;
            end;
            Writeln;
    end;
    Procedure MostrarIzq(Lista: T_Lista);
    begin
        while (Lista.Cola <> nil) do
            begin
            Writeln(Lista.Cola^.int, ' ');
            Lista.Cola := Lista.Cola^.ant;
            end;
        Writeln;
    end;
    Procedure CreaD(var Lista:T_Lista);
    var Aux1, Aux2: ListEnt;
    begin
    Aux1 := Lista.Cbeza;
    while (Aux1 <> nil) do
        begin
        Aux2 := Lista.Cola;
        New(Lista.Cola);
        Lista.Cola := Aux1;
        Lista.Cola^.ant := Aux2;
        Aux1 := Aux1^.sig;
        end;
    end;
var Lista: T_Lista;
begin
Clrscr;
Crear(Lista);
CreaD(Lista);
MostrarDer(Lista);
MostrarIzq(Lista);
Readln;
end.

Should it be done differently?

    
asked by Lara Cesio 28.05.2017 в 22:30
source

1 answer

1

From what I see, this is an exercise of "Linked Lists". The concept of these lists is: We have a data type that contains a pointer to the previous, next address and the record of what we want to save. I'm going to concentrate on two procedures that you created: Create and Show. To create, in the first cycle, we reserve memory for our first record. "Previous" will be null and we will save the address that points to the "Header" of the first pointer. in the other registers we assign the "following" variables. When we finish reading the file we get the position of the "Cola".

the code for this would be something like this:

Procedure Crear(var Lista: T_Lista);
    var 
        Arch: Text; 
        NuevoDato, Ant,Act: ListEnt;
        primero:Boolean;
    begin
        Lista.Cbeza := nil;
        Lista.Cola := nil;
        Assign(Arch, 'ENTEROS.txt');
        Reset(Arch);
        Ant:=nil;
        Act:=nil;
        Primero:=True;

        while not Eof(Arch) do
        begin
            if primero then
            begin
                primero:=False;
                New(NuevoDato);
                Readln(Arch, NuevoDato^.int);
                NuevoDato^.ant:=nil;
                Lista.Cbeza:=NuevoDato;
            end
            else
            begin
                New(NuevoDato);
                Readln(Arch, NuevoDato^.int);
                act:=NuevoDato;
                NuevoDato:=Ant;
                NuevoDato^.Sig:=act;
                NuevoDato:=Act;
                NuevoDato^.Ant:=ant;
            end;
            ant:=nuevodato;                       
        end;    
        Close(Arch);
        Lista.Cola:=NuevoDato;  
    end;

Keep in mind that procedural variables variables never used them as such.

To show the elements, the pointer must be positioned for example in the first position and go to the end of the list.

For example:

        NuevoDato:=Lista.Cbeza;
        primero:=true;
        while primero do
        begin
            if NuevoDato=Lista.Cola then primero:=false;

            writeln(Nuevodato^.int);
            Nuevodato:=Nuevodato^.sig;
        end;

Particularly, in this exercise, I would take as reference two global variables for each of the list and two global variables for the "Head" and "Tail". Once these concepts are clear you can make things more difficult.

    
answered by 29.05.2018 в 13:49