sábado, 23 de abril de 2011

Estrutura de dados

 Trabalho feito no semestre passado, matéria de estrutura de dados.
Contém o procedimento remover das listas duplamente e simplesmente encadeada:

program PrjTrabalho_2bim;

{$APPTYPE CONSOLE}

uses SysUtils;

type TpPont =^Lista;
  Lista = record
    info:string;
    prox:TpPont;
  end;

type TpPontDupla =^ListaDupla;
  ListaDupla = record
    info:string;
    aux,prox,ant:TpPontDupla;
  end;

Procedure CriaListaDupla(var L:tpPontDupla);
begin
  L:=nil;
end;

Procedure InsereDupla(var L:TpPontDupla; valor: string);
var p,aux:TpPontDupla;
begin
  new(p);
  p^.info:=valor;
  if(L = nil)then
    begin
      p^.prox:=nil;
      p^.ant:=nil;
      L:=p;
    end
  else
    begin
      p^.prox:=l;
      p^.ant:=nil;
      aux:=l;
      aux^.ant:=p;
      l:=p;
    end;
end;

procedure CriaListaSimples(var L:tpPont);
begin
  L:=nil;
end;

Procedure InsereSimples(Var L:tpPont; Valor: String);
var p:TpPont;
begin
  new(p);
  p^.info:=valor;
  p^.prox:=L;
  L:=p;
end;

Procedure ExibirListaDupla(L:TpPontDupla);
begin
  while L <> nil do
  begin
    writeln(L^.info);
    L:=L^.prox;
  end;
end;

Procedure ExibirListaSimples(L:TpPont);
begin
  while L <> nil do
  begin
    writeln(L^.info);
    L:=L^.prox;
  end;
end;

Procedure Remove_PrimDupla(Var L: TpPontDupla);
var p:TpPontDupla;
Begin
p := L;
L:= L^.prox;
dispose(p)
End;

Procedure Remove_PrimSimples(Var L: TpPont);
var p:TpPont;
Begin
p := L;
L:= L^.prox;
dispose(p)
End;

var resp:char; valor:string; L:TpPont; L2:TpPontDupla;
begin
  while resp <> '0' do
    begin
      writeln('Menu');
      writeln('1 - Lista Simples');
      writeln('2 - Lista Dupla');
      writeln('0 - Sair');
      readln(resp);
      if resp = '1' then
        begin
          CriaListaSimples(L);
          resp:='s';
          while resp = 's' do
            begin
              writeln('Digite um valor para lista:');
              readln(valor);
              InsereSimples(L,valor);
              writeln('Deseja inserir mais algum valor? (s/n)');
              readln(resp);
            end;
          if resp = 'n' then
            begin
              writeln('O primeiro valor da lista que foi o ultimo a ser inserido sera removido');
              Remove_PrimSimples(L);
              writeln('O restante dos valores inseridos na lista sao:');
              ExibirListaSimples(L);
            end;
        end
      else if resp = '2' then
        begin
          CriaListaDupla(L2);
          resp:='s';
          while resp = 's' do
            begin
              writeln('Digite um valor para lista:');
              readln(valor);
              InsereDupla(L2,valor);
              writeln('Deseja inserir mais algum valor? (s/n)');
              readln(resp);
            end;
          if resp = 'n' then
            begin
              writeln('O primeiro valor da lista que foi o ultimo a ser inserido sera removido');
              Remove_PrimDupla(L2);
              writeln('O restante dos valores inseridos na lista sao:');
              ExibirListaDupla(L2);
            end
      end
        else if resp = '0' then
          begin
            writeln('Deseja realmente sair? (s/n)');
            readln(resp);
            if resp = 's' then
              resp:='0';
          end;
    end;
end.

Nenhum comentário:

Postar um comentário