Operaciones con listas especiales - UTN

Anuncio
Sintaxis y Semántica del Lenguaje – Apunte de práctica 2014
Operaciones con listas especiales: Listas Dobles y Circulares.
Lista Doblemente encadenada.
Type
listaD= ^nodoD
nodoD= record
dato: elem; {puede ser de cualquier tipo}
pant: listaD;
psig: listaD
end;
Procedure Crear (var LD: listaD);
Begin
LD:=nil
End;
Procedure InsPcpio (var LD: listaD; e: elem);
Var
Nuevo:listaD;
Begin
New(nuevo);
nuevo^.dato:=e;
nuevo^.pant:=nil;
nuevo^.psig:=LD;
if (LD <>nil) then
LD^.pant:=nuevo;
LD:=nuevo
End;
Procedure Insfin (var LD: listaD; e: elem);
Var
Pc, Nuevo:listaD;
Begin
New(nuevo);
nuevo^.dato:=e;
nuevo^.psig:=nil;
if (LD =nil) then
{lista vacía}
begin
nuevo^.pant:=nil;
LD:=nuevo
End
Else
Begin
Pc:=LD;
{recorrer hasta el último nodo}
While (pc^.psig <> nil) do
Pc:=pc^.psig;
Pc^.psig:= nuevo;
1
Sintaxis y Semántica del Lenguaje – Apunte de práctica 2014
nuevo^.pant:=pc
end
End;
Procedure InsfOrd (var LD: listaD; e: elem);
Var
Paux, Pc, Nuevo: listaD;
Begin
New(nuevo);
nuevo^.dato:=e;
Pc:=LD;
while (pc <> nil) and (pc^.dato < e) do
Pc:=pc^.psig;
if (LD=nil) or (pc^.pant =nil) then
begin
nuevo^.pant:=nil;
nuevo^.psig:=LD;
LD:=nuevo
End
Else
Begin
Nuevo^.psig:=pc;
Pc^.pant:= nuevo;
Paux:= pc^.pant;
Paux^.psig:= nuevo;
nuevo^.pant:=paux
end
End;
{inserta al pcipio}
{inserta en el cuerpo}
Procedure Borrar (var LD: listaD; e: elem);
Var
Paux, Pc: listaD;
Begin
Pc:=LD;
while (pc <> nil) and (pc^.dato < >e) do
Pc:=pc^.psig;
if (pc<>nil) then
begin
if (pc^.pant =nil) then
begin
LD:=LD^.psig;
If (LD <> nil) then
LD^.pant:=nil
End
Else
Begin
Paux:= pc^.pant;
Paux^.psig:= pc^.psig;
If (pc <> nil) then
2
{borra el primer elemento}
{borra en el cuerpo}
Sintaxis y Semántica del Lenguaje – Apunte de práctica 2014
Pc^.psig.pant:= paux
End;
Dispose(pc)
end
End;
Lista Circular.
Type
listaC= ^nodoC
nodoC= record
dato: elem; {genérico}
psig: listaC
end;
Procedure Crear (var LC: listaC);
Begin
LC:=nil
End;
Procedure InsPcpio (var LC: listaC; e: elem);
Var
Pc, Nuevo:listaC;
Begin
New(nuevo);
nuevo^.dato:=e;
if (LC =nil) then
{lista vacía}
begin
LC:=nuevo;
nuevo^.psig:=LC
{se engancha consigo misma}
end
else
{recorrer hasta el último nodo}
begin
pc:=LC;
While (pc^.psig <> LC) do
Pc:=pc^.psig;
nuevo^.psig:= LC;
{pone nuevo al principio}
LC:=nuevo;
{define el inicio de la lista}
Pc^.psig:=LC
{engancha el último con el inicio de la lista}
end
End;
Procedure Insfin (var LC: listaC; e: elem);
Var
Pc, Nuevo:listaC;
Begin
New(nuevo);
nuevo^.dato:=e;
if (LC =nil) then
{lista vacía}
3
Sintaxis y Semántica del Lenguaje – Apunte de práctica 2014
begin
LC:=nuevo;
nuevo^.psig:=LC;
End
Else
Begin
Pc:=LC;
{recorrer hasta el último nodo}
While (pc^.psig <> LC) do
Pc:=pc^.psig;
Pc^.psig:= nuevo;
nuevo^.psig:=LC
end
End;
Procedure InsOrd (var LC: listaC; e: elem);
Var
Pa, Pc, Nuevo: listaC;
Begin
New(nuevo);
nuevo^.dato:=e;
if (LC=nil) then
{lista vacía}
begin
LC:=nuevo;
nuevo^.psig:=LC
End
Else
If (e < LC^.dato) then {inserto al pcipio}
begin
Pc:=LC;
while (pc^.psig <> LC) do
Pc:=pc^.psig;
Nuevo^.psig:=LC;
LC:=nuevo;
Pc^.psig:=nuevo
End
Else
{busco donde insertar ordenado en cuerpo}
Begin
pa:=LC;
Pc:= LC^.psig;
While (pc <> LC) and ( pc^.dato < e) do
Begin
Pa:=pc;
Pc:=pc^.psig
End
Pa^.psig:=nuevo;
Nuevo^.psig:=pc
End
End;
4
Sintaxis y Semántica del Lenguaje – Apunte de práctica 2014
Procedure Borrar (var LC: listaC; e: elem);
Var
Pa, Pc: listaC;
Begin
if (LC<> nil) then
If (e = LC^.dato) then
{borra el pcipio}
begin
Pc:=LC;
If (pc^.psig = LC) then
{único nodo}
LC:=nil
else
begin
while (pc^.psig <> LC) do
Pc:=pc^.psig;
LC:=LC^.psig;
Pc^.psig:=LC;
Dispose(pc)
End
end
Else
{borra en el cuerpo}
Begin
pa:=LC;
Pc:= LC^.psig;
While (pc <> LC) and ( pc^.dato < >e) do
Begin
Pa:=pc;
Pc:=pc^.psig
End
Pa^.psig:=pc^.psig;
Dispose(pc)
End
End;
Procedure imprimir (LC:listaC);
Var
Pc:listaC;
Begin
Pc:=LC;
If (pc <> nil) then
Begin
Writeln(pc^.dato);
{imprime 1º elemento}
Pc:=pc^.psig;
While (pc <> LC) do
Begin
Writeln(Pc^.dato);
Pc:=pc^.psig
End
End
5
Sintaxis y Semántica del Lenguaje – Apunte de práctica 2014
Else
writeln(‘lista vacia’)
End;
6
Descargar