0 votes

Double List in Pascal

I am doing a list exercise. The exercise asks me to create a double list of integers. I am having a hard time doing it and I think I did it wrong. I read the integers from a file making 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.

It must be done differently, right?

1voto

Hernan L. Points 61

As far as I can see, this is a "Linked Lists" exercise. The concept of these lists is: We have a data type that contains a pointer to the previous address, next address and the record of what we want to save. I am going to concentrate on two procedures you created: Create and Show. To create, in the first loop, we reserve memory for our first record. "Previous" is going to be null and we are going to store the address that points to the "Header" of the first pointer. in the other registers we assign the "next" variables. When we finish reading the file we get the position of the "Queue".

the code for this would look 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;

Note that you never used the procedure argument variables as such.

To display the elements, the pointer must be positioned, for example, at the first position and scroll 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.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X