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?