Algoritmo para repassar os elementos de uma fila, sendo que no final do processamento, os elementos da fila devem estar disposto em uma lista encadeada (pascalzim 5.1.2)
Autor: Amauri | Publicado: 2011-10-21T15:50:00-03:00

Program Pzim ;
&nbsp;const max = 7;
&nbsp;type Fila = record
&nbsp;  dados : array[1..max]of string;
&nbsp; &nbsp; &nbsp; inicio, fim : integer;
&nbsp; &nbsp; &nbsp;end;
&nbsp; 

  &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;
var
&nbsp; L :ListEnc;
&nbsp; &nbsp; &nbsp;f1 : Fila;
&nbsp; &nbsp; &nbsp;v: string;
&nbsp; &nbsp; 
&nbsp;procedure criar(var F:Fila);
&nbsp;begin
&nbsp; &nbsp; &nbsp;F.inicio := 1;
&nbsp; &nbsp; &nbsp;F.fim := 1;
&nbsp;end;

&nbsp;function filaVazia(F:Fila):boolean;
&nbsp;begin
&nbsp; &nbsp; &nbsp;if F.inicio = F.fim then
&nbsp; &nbsp; &nbsp; filaVazia := true
&nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp; filaVazia := false;
&nbsp;end;

&nbsp;function filaCheia(F:Fila):boolean;
&nbsp;begin
&nbsp; &nbsp; &nbsp;if F.fim &gt; max then
&nbsp; &nbsp; &nbsp; filaCheia := true
&nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp; filaCheia := false;
&nbsp;end;

&nbsp;procedure enqueue(var F:Fila;s:string);
&nbsp;begin
&nbsp; &nbsp; &nbsp;if filaCheia(F)then
&nbsp; &nbsp; &nbsp; writeln('A fila tá cheia!')
&nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp;begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;F.dados[F.fim] := s;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;F.fim := F.fim + 1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writeln('Inserção efetuada!')
&nbsp; &nbsp; &nbsp;end;
&nbsp;end;

&nbsp;function dequeue(var F:Fila):string;
&nbsp;begin
&nbsp; &nbsp; &nbsp;if filaVazia(F)then
&nbsp; &nbsp; &nbsp; writeln('A fila tá vazia!')
&nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp;begin
&nbsp; &nbsp; &nbsp; dequeue := F.dados[F.inicio];
&nbsp; &nbsp; &nbsp; F.inicio := F.inicio + 1;
&nbsp; &nbsp; &nbsp;end;
&nbsp;end;

&nbsp;procedure mostrarFila(F:Fila);
&nbsp;begin
&nbsp; &nbsp; &nbsp;if filaVazia(F)then
&nbsp; &nbsp; &nbsp; writeln('A fila tá vazia!')
&nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp;begin
&nbsp; &nbsp; &nbsp; while not filaVazia(F)do
   write(dequeue(F),'|');
&nbsp; &nbsp; &nbsp;end;
&nbsp; &nbsp; &nbsp;writeln;
&nbsp;end;
&nbsp; &nbsp;procedure Lcriar(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; 

&nbsp;Begin
&nbsp; &nbsp; &nbsp;textcolor(white);
 criar(f1);
 Lcriar(L);
&nbsp; &nbsp; &nbsp;writeln('A fila está vazia? ', filaVazia(f1));
&nbsp; &nbsp; &nbsp;writeln('A fila está cheia? ', filaCheia(f1));
&nbsp; &nbsp; &nbsp;enqueue(f1,'Macaco');
&nbsp; &nbsp; &nbsp;enqueue(f1,'Cachorro');
&nbsp; &nbsp; &nbsp;enqueue(f1,'Gato');
&nbsp; &nbsp; &nbsp;enqueue(f1,'Coruja');
&nbsp; &nbsp; &nbsp;enqueue(f1,'Cobra');
&nbsp; &nbsp; &nbsp;enqueue(f1,'Porco');
&nbsp; &nbsp; while not filaVazia(f1) do
&nbsp; &nbsp; &nbsp;begin 
  &nbsp;v := dequeue(f1);
&nbsp; &nbsp; &nbsp; &nbsp;inserir(L, v);
&nbsp; &nbsp; &nbsp;end;
&nbsp; &nbsp; &nbsp;writeln ('imprimindo a lista');
  &nbsp;imprimir(L);
&nbsp; &nbsp; &nbsp;readln;
&nbsp;End.