Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {======================================================================================}
- {
- Autor: Mario Günter Simão
- Nome do arquivo: trabalhoAc.pas
- Tamanho do TAB: 4
- Data: 14.12.2016
- Atualização: 14.12.2016
- Objetivo: Aprimoramento dos procedimentos envolvendo árvore
- binária, impedindo de adicionar na árvore itens
- já existentes e impressão dos itens usando pilha,
- sem usar recursividade.
- Observação: Não usar o vi/vim para ler o código fonte, devido
- aos problemas que apresenta em comentários longos
- de uma só linha.
- }
- {======================================================================================}
- program trabalhoAc;
- uses crt;
- type
- noAgenda = ^regAgenda;
- regAgenda = record
- nome: string[30];
- telefone: string[16];
- esq, dir: noAgenda;
- end;
- {======================================================================================}
- // Verifica se o contato já existe na árvore binária
- // Se existe, retorna true
- // Se não existe, retorna false
- function contatoExiste (var atual: noAgenda; novo: noAgenda): boolean;
- begin
- if atual = nil then atual:= novo
- else
- begin
- if novo^.nome <> atul^.nome then contatoExiste(atual^.dir,novo)
- else contatoExiste(atual^.esq,novo);
- end;
- end;
- procedimento buscarLugar(var atual: noAgenda; novo: noagenda)
- (* buscar o lugar na árvores descendo até um nó livre;
- ATENÇÃO: o primeiro parâmetro é por referência, o que é necessário para o novo nó ser ligado à árvore *)
- inicio
- se atual = nil então
- atual ← novo
- senão se novo^.nome > atual^.nome então
- buscarLugar(atual^.dir,novo)
- senão buscarLugar(atual^.esq,novo)
- fim
- {======================================================================================}
- // Inclui novo contato na árvore binária
- // Se o contato já existe: não inclui + mensagem
- procedure procIncluir(var raiz,atual: noAgenda);
- var
- novo: noAgenda;
- nome: string[30];
- telefone: string[16];
- begin
- new(novo);
- atual:= raiz;
- write('Nome: ');
- readln(nome); // FAZER TRATAMENTO
- write('Telefone: ');
- readln(telefone); // FAZER TRATAMENTO
- novo^.nome:= nome;
- novo^.telefone:= telefone;
- novo^.esq:= nil;
- novo^.dir:= nil;
- if (contatoExiste(raiz,novo) = true) then
- begin
- if raiz = nil then raiz:= novo
- else buscarLugar(raiz,novo);
- atual:= novo;
- end
- else
- begin
- writeln('Nome já existe.');
- end;
- end;
- {======================================================================================}
- { P R O G R A M A P R I N C I P A L }
- {======================================================================================}
- var
- opcao: integer;
- raiz, atual, novo, aux: noAgenda;
- begin
- raiz:= nil;
- repeat
- begin
- clrscr;
- writeln;
- writeln('________________________________________________');
- writeln('| |');
- writeln('| M E N U |');
- writeln('|______________________________________________|');
- writeln('| | |');
- writeln('| 1 | Adicionar Contato |');
- writeln('| 2 | Opção 02 |');
- writeln('| 3 | Opção 03 |');
- writeln('| 4 | Opção 04 |');
- writeln('| 5 | Opção 05 |');
- writeln('| | |');
- writeln('| 0 | Sair |');
- writeln('|___|__________________________________________|');
- writeln;
- write('Opção: ');
- readln(opcao);
- case opcao of
- 1:
- begin
- end;
- 2:
- begin
- end;
- 3:
- begin
- end;
- 4:
- begin
- end;
- 5:
- begin
- end;
- 0:
- begin
- clrscr;
- end;
- end;
- end;
- until opcao = 0;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement