Lista encadeada com uma função de busca que retorna true se o elemento procurado estiver na lista e false caso contrário (pascalzim 5.1.2)
Autor: Amauri | Publicado: 2011-10-21T15:55:00-03:00

program ListaEncadeada;
&nbsp; &nbsp; &nbsp; &nbsp; 
  &nbsp; &nbsp;type
  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListEnc = ^No;
  &nbsp; 
   No = record
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj: String;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prox: ListEnc;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;
&nbsp; &nbsp; &nbsp;var
&nbsp; L :ListEnc;
&nbsp; bus : string;

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

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

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

&nbsp;//INICIO DO PROGRAMA PRINCIPAL

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