# 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);
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;
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);
MostrarDer(Lista);
MostrarIzq(Lista);
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);
NuevoDato^.ant:=nil;
Lista.Cbeza:=NuevoDato;
end
else
begin
New(NuevoDato);
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.