Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lista_Ordenada ;
- Type Palavra=string[15];
- P_Lista=^TipoNo;
- TipoNo=record
- info:palavra;
- prox:P_lista;
- end;
- var Pi:P_lista;
- Op:char;
- {Procedimentos para desenvolver}
- Procedure Inserir;
- Var Px:P_lista;
- Begin
- Px:=Pi;
- //P. auxiliar aponta inicio lista
- new(Px);
- write('Escreva dado a inserir na lista: ');
- readln(Px^.info);
- Px^.prox:=Pi;
- Pi:=Px;
- writeln('Novo dado inserido na lista');
- End;
- Procedure percorrer;
- Var Px:P_lista;
- begin
- writeln;
- Px:=Pi;
- //P. auxiliar aponta inicio lista
- If (Px <> nil) then
- writeln ('Lista vazia')
- else
- Begin
- writeln('LISTA: '); writeln;
- while (Px <> nil) do
- begin
- writeln(Px^.info);
- Px:=Px^.prox;
- end;
- end;
- end;
- Procedure Remover;
- var px: P_lista;
- pa: P_lista;
- //Ponteiro para nó anterior
- //do nó a remover
- dado:palavra;
- Begin
- if pi <> nil then
- begin
- write('Escreva dado a excluir da lista: ');
- readln(dado);
- px:=pi;
- pa:=nil;
- while (px <> nil) and
- (px^.info <> dado) do
- begin
- pa:=px;
- //Atualiza ponteiro
- //antecessor
- px:=px^.prox;
- //avança para próx. nó
- end;
- end;
- if px = nil then
- writeln(dado, ' não se encontra na lista')
- else
- begin
- if pa = nil
- //Caso em que vamos
- //excluir 1º nó da Lista
- then pi:=px^.prox
- //redefine-se o início da
- //lista
- else pa^.prox:=px^.prox;
- //Antecessor a apontar o
- //novo prox (próximo nó)
- dispose(px);
- writeln(dado, ' foi excluído da lista');
- end;
- end;
- Procedure Ordenar;
- Var p1,p2:P_lista;
- trocar:boolean;
- aux:palavra;
- begin
- if (pi <> nil) and
- (pi^.prox <> nil) then
- repeat
- trocar:=false;
- p1:=pi;
- p2:=pi^.prox;
- repeat
- if (p1^.info > p2^.info)
- then
- begin
- trocar:=true;
- aux:=p1^.info;
- p1^.info:=p2^.info;
- p2^.info:=aux;
- end;
- p1:= p1^.prox;
- p2:= p2^.prox;
- until pi^.prox=nil;
- until not trocar;
- end;
- Begin //Programa principal
- Pi:=nil;
- Repeat
- writeln('Menu');
- writeln('Inserir --> 1');
- writeln('Percorrer --> 2');
- writeln('Ordenar --> 3');
- writeln('Remover --> 4');
- writeln('Terminar --> 0');
- readln(op);
- Case op of
- '1' : Inserir;
- '2' : Percorrer;
- '3' : Ordenar;
- '4' : Remover;
- '0' : writeln('Fim');
- End;
- Until Op = '0';
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement