Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Lista Enlazada (https://www.clubdelphi.com/foros/showthread.php?t=82682)

blackx5n 03-04-2013 06:08:31

Lista Enlazada
 
Hola a todos, quiero hacer una lista enlazada que contenga datos enteros. Tengo varios problemas

1- Como puedo crear un procedimiento o funcion, donde pueda crear el primero nodo y posteriormente se vallan agregando los siguientes nodos.
Realize un procedimiento llamado insertar, pero creo que no es la forma correcta de hacerlo no he logrado agregar ningun dato ala lista.

2- Como puedo recorrer la lista ala inversa e imprimirla

Espero que alguno de ustedes me proporcione alguna idea o algun ejemplo de como realizar esto.


Código Delphi [-]
program Project1;

{$APPTYPE CONSOLE}

uses
  crt;

type
   pNodo = ^Nodo;

   Nodo = record
     dato:Integer;
     Sig,Ant:pNodo; // Sig: Siguiente   Ant:Anterior
   end;

var
   lista:pNodo;
   firt:Pointer;
   j:Integer;


 // Imprime lista
procedure Imprimir(x:pNodo);
begin
     while x<>nil do
     begin
      Writeln(x^.dato);
      x:=x^.Sig;
     end;
end;


procedure insertar(dat:Integer; vf:pNodo);
var
    f:Pointer;
    j,n:integer;
begin
   New(vf);
   vf^.dato:=dat;
   vf^.Sig:=nil;
   for j:=1 to 3 do
   begin
   f:=vf;
   New(vf);
   vf^.dato:=dat;
   vf^.Sig:=f;
   end;
end;


begin
  // Primer Nodo
  New(lista);
  lista^.dato:=23;
  lista^.Sig:=nil;

  firt:=lista;
  New(lista);
  lista^.dato:=14;
  lista^.Sig:=firt;

  firt:=lista;
  New(lista);
  lista^.dato:=18;
  lista^.Sig:=firt;

  firt:=lista;
  New(lista);
  lista^.dato:=97;
  lista^.Sig:=firt;

  {
  lista:=nil;
  for j:=1 to 3 do
  insertar(j,lista);
  }

  ClrScr;
  Writeln('Lista Enlazada');
  imprimir(lista);

  ReadKey;
end.

ecfisa 03-04-2013 09:19:44

Cita:

Empezado por blackx5n (Mensaje 457973)
Hola a todos, quiero hacer una lista enlazada que contenga datos enteros. Tengo varios problemas

1- Como puedo crear un procedimiento o funcion, donde pueda crear el primero nodo y posteriormente se vallan agregando los siguientes nodos.
Realize un procedimiento llamado insertar, pero creo que no es la forma correcta de hacerlo no he logrado agregar ningun dato ala lista.

2- Como puedo recorrer la lista ala inversa e imprimirla

Hola blackx5n.

Código Delphi [-]
...
type
  pNodo = ^TNodo;

  TNodo = record
    dato: Integer;
    ant,
    sig : pNodo;
  end;

// Crear Lista 
procedure CrearLista(var lst: pNodo; valor: Integer);
begin
  New(lst);
  lst^.dato := valor;
  lst^.ant  := nil;
  lst^.sig  := nil
end;

// Agregar un nodo 
procedure AgregarNodo(var lst: pNodo; valor: Integer);
var
  ndo: pNodo;
begin
  New(ndo);
  ndo.dato := valor;
  lst.sig  := ndo;
  ndo.ant  := lst;
  ndo.sig  := nil;
  lst      := ndo
end;

// Mostrar invertida 
procedure MostrarInvertida(var lst: pNodo);
var
  aux : pNodo;
begin
  aux := lst;
  while lst.sig <> nil do lst := lst.sig; 
  while lst <> nil do
  begin
    write(lst.dato:3);
    lst := lst.ant
  end;
  lst := aux
end;

// Liberar
procedure LiberarLista(var lst: pNodo);
begin
  Dispose(lst)
end;

// Ej.: Crear lista, Insertar elementos y mostrar invertidos
var
  i: Integer;
  Lista: pNodo;
begin
  CrearLista(Lista, 1);
  for i:= 2 to 20 do
     AgregarNodo(Lista, i);
  MostrarInvertida(Lista);
  LiberarLista(Lista);
  readln
end.

Saludos.

blackx5n 03-04-2013 09:37:10

Lista Enlazada
 
Muchas gracias ecfisa, es lo que estaba buscando.

Problema resuelto mil gracias.

ecfisa 03-04-2013 21:24:30

De nada blackx5n.

Pero hacía mucho tiempo que no implementaba listas...:o, voy a rectificar el código de liberación de la lista:
Código Delphi [-]
// Liberar
procedure LiberarLista(var lst: pNodo);
var
  act,sig: pNodo;
begin
  act := lst;
  while act <> nil  do
  begin
    sig := act.sig;
    Dispose(act);
    act := sig;
  end;
end;

Saludos. :)


La franja horaria es GMT +2. Ahora son las 22:57:57.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi