program rotinas_de_vetor;

Const
  Maximo=10;

Type
  Tipo_Dado = Integer;

  Tipo_Vetor = Record
                 Dado       : Array [1..Maximo] of Tipo_Dado;
                 Excluido   : Array [1..Maximo] of Boolean;
                 Inicio,Fim : Integer;
               End;

{
  Rotinas de Manipula‡Æo de Vetores - Lista Linear Sequencial:

Proc Inicializa     (var V:Tipo_Vetor);
Func Insere_Vetor   (var V:Tipo_Vetor; Dado:Tipo_Dado):Boolean;
Func Consulta_Vetor (V:Tipo_Vetor; Indice:Integer; var Dado:Tipo_Dado):Boolean;
Func Acha_Vetor     (V:Tipo_Vetor; Dado:Tipo_Dado; var Indice:Integer):Boolean;
Proc Lista_Vetor    (V:Tipo_Vetor);
Func Exclui_Vetor   (var V:Tipo_Vetor; Indice:Integer):Boolean;
Func Atualiza_Vetor (var V:Tipo_Vetor; Indice:Integer; Novo_Dado:Integer):Boolean;
Proc Compacta_Vetor (var V:Tipo_Vetor);
Func Vazio_Vetor    (V:Tipo_Vetor):Boolean;
Func Quantidade_Vetor (V:Tipo_Vetor):Integer;
}

Procedure Inicializa (var V:Tipo_Vetor);
var
  cont:integer;
begin
  V.inicio:=1;
  V.fim:=0;
  for cont:=1 to Maximo
  do V.excluido[cont]:=false;
end;

Function Insere_Vetor (var V:Tipo_Vetor; Dado:Tipo_Dado):Boolean;
begin
  if V.fim < Maximo
  then begin
         V.fim:=V.fim+1;
         V.dado[V.fim]:=dado;
         V.excluido[V.fim]:=false;
         insere_vetor:=true;
       end
  else insere_vetor:=false;
end;

Function Consulta_Vetor (V:Tipo_Vetor; Indice:Integer; var Dado:Tipo_Dado):Boolean;
begin
  if (indice < V.inicio) or (indice > V.fim) or (V.excluido[indice] = true)
  then consulta_vetor:=false
  else begin
         dado:=V.dado[indice];
         consulta_vetor:=true;
       end;
end;

Function Acha_Vetor (V:Tipo_Vetor; Dado:Tipo_Dado; var Indice:Integer):Boolean;
var
  cont:integer;
  achou:boolean;
begin
  achou:=false;
  for cont:=V.inicio to V.fim
  do if (V.dado[cont] = dado) and (V.excluido[cont] = false)
     then begin
            achou:=true;
            indice:=cont;
          end;
  if achou
  then acha_vetor:=true
  else acha_vetor:=false;
end;

Procedure Lista_Vetor (V:Tipo_Vetor);
var
  dado:Tipo_Dado;
  cont:integer;
begin
  writeln;
  for cont:=V.inicio to V.fim
  do if consulta_vetor(V,cont,dado)
     then writeln('Vetor[',cont,']=',dado)
     else writeln('Vetor[',cont,']= <Excluido>');
  writeln;
end;

Function Exclui_Vetor (var V:Tipo_Vetor; Indice:Integer):Boolean;
begin
  if (indice < V.inicio) or (indice > V.fim)
  then exclui_vetor:=false
  else begin
         V.excluido[indice]:=true;
         exclui_vetor:=true;
       end;
end;

Function Atualiza_Vetor (var V:Tipo_Vetor; Indice:Integer; Novo_Dado:Integer):Boolean;
begin
  if (indice < V.inicio) or (indice > V.fim) or (V.excluido[indice] = true)
  then atualiza_vetor:=false
  else begin
         V.dado[indice]:=novo_dado;
         atualiza_vetor:=true;
       end;
end;

Procedure Compacta_Vetor (var V:Tipo_Vetor);
var
  cont:integer;
begin
  cont:=V.inicio;
  while (cont <= V.fim)
  do begin
       if V.excluido[cont] = true
       then begin
              V.excluido[cont]:=V.excluido[V.fim];
              V.dado[cont]:=V.dado[V.fim];
              V.excluido[V.fim]:=false;
              V.fim:=V.fim-1;
              if V.excluido[cont] = false
              then cont:=cont+1;
            end
       else cont:=cont+1;
     end;
end;

Function Vazio_Vetor (V:Tipo_Vetor):Boolean;
begin
  if V.fim >= V.inicio
  then vazio_vetor:=false
  else vazio_vetor:=true;
end;

Function Quantidade_Vetor (V:Tipo_Vetor):Integer;
begin
  quantidade_vetor:=V.fim;  {Ou... V.fim-V.inicio+1 }
end;

{ Programa Principal }

var
  Vet:Tipo_Vetor;
  Valor:Integer;

begin
  inicializa(Vet);

  if Vazio_Vetor(Vet)
  then writeln('=> Vetor vazio...')
  else begin
         writeln('=> Elementos do vetor...',quantidade_vetor(Vet));
         lista_vetor(Vet);
       end;

  if not insere_vetor(Vet,2)
  then writeln('>>> Erro: Insere vetor! ');

  if not insere_vetor(Vet,4)
  then writeln('>>> Erro: Insere vetor!');

  if not insere_vetor(Vet,6)
  then writeln('>>> Erro: Insere Vetor!');

  if Vazio_Vetor(Vet)
  then writeln('=> Vetor vazio...')
  else begin
         writeln('=> Elementos do vetor...',quantidade_vetor(Vet));
         lista_vetor(Vet);
       end;

  writeln('>> Pressione <enter> para sair do programa <<');
  readln;

end.

