-->

Seguidores

Tecnologia do Blogger.
Cursos Online na Área de Informática
Siga-nos
Twitter Facebook RSS
sexta-feira, 21 de outubro de 2011
program ListaEncadeada;
       
   type
         ListEnc = ^No;
 
No = record
                  obj: String;
                  prox: ListEnc;
             end;
     var
  L :ListEnc;
  bus : string;

         //CRIA LISTA VAZIA
         procedure criar(var L:ListEnc);
         begin
             L := nil;
         end;
       
   //INFORMA SE A LISTA ESTÁ VAZIA OU NÃO
         function vazia(L: ListEnc):boolean;
         begin
             if L = nil then
                vazia := true
             else
                vazia := false;
         end;

         //INSERE UM ELEMENTO NA LISTA
         procedure inserir(var L: ListEnc; s: string);
         var
            N, P: ListEnc;
          begin
            new(N);
            N^.obj := s;
            if vazia(L)then
               begin
                    N^.prox := L;
                    L := N;
               end
           else
  begin
          P := L;
          while (P^.prox <> nil)do
                          P := P^.prox;
          N^.prox := P^.prox;
          P^.prox := N;
  end;
  end;
 
//REMOVE UM ELEMENTO DA LISTA
          function remover(var L: ListEnc; s: string):boolean;
          var
            P, Q: ListEnc;
          begin
           if vazia(L)then
              remover := false
           else
            if (L^.obj = s)then
            begin
              P := L;
              L := L^.prox;
              dispose(P);
              remover := true;
            end
            else
               begin
                P := L;
                    while ((P^.prox <> nil)and (P^.prox^.obj <> s)) do
                    begin
                        P := P^.prox;
                    end;
                    if (P^.prox <> nil) and (P^.prox^.obj = s) then
                    begin
                         Q := P^.prox;
                         P^.prox := Q^.prox;
                         dispose(Q);
                         remover := true;
                    end
                    else
                    begin
                        remover := false;
                    end;
end;        
end;

         // IMPRIME A LISTA
         procedure imprimir(L: ListEnc);
         var
            P: ListEnc;
         begin
            if vazia(L)then
               writeln('LISTA VAZIA!!!')
            else
            begin
                P := L;
                while (P <> nil) do
                begin
write(P^.obj,' | ');
                     P := P^.prox;
                end;
            end;
         end;
   function busca(L: ListEnc; x: string):boolean;
         var
            P: ListEnc;
         begin
                P := L;
                busca := false;
                while (P <> nil) do
                begin
if P^.obj = x then
   busca := true;
                     P := P^.prox;
                end;
            end;          

 //INICIO DO PROGRAMA PRINCIPAL

 Begin
   criar(L);
 writeln(vazia(L));
 inserir(L, 'MACACO');
 inserir(L, 'CACHORRO');
 inserir(L, 'GATO');
 inserir(L, 'LEÃO');
 inserir(L, 'RATO');
 inserir(L, 'CAMELO');
 inserir(L, 'CORUJA');
 imprimir(L);
 writeln;
 writeln(remover(L,'RATO'));
 writeln('APÓS A REMOÇÃO...');
 writeln;
 imprimir(L);

 writeln ('busca');
 readln (bus);
 writeln (busca(L, bus));
 readln;
 End.
Gostou? Compartilhe!

0 comentários:

Seu Sistema Operacional é...

TEMAS

Visualizações