-->

Seguidores

Tecnologia do Blogger.
Cursos Online na Área de Informática
Siga-nos
Twitter Facebook RSS
quinta-feira, 20 de outubro de 2011
                               Program filapilha ;
                  // Autor: Cícero Amauri
                                   { Escreva um algoritmo que converta uma pilha implementada em:
·        Fila, Considerar a lista com no máximo 100 elementos} 

const max = 100;
type 
    pilha = record
            obj: array [1..max] of integer;
            cont: integer;
        end;
        
        fila = record
            dados : array [1..max] of integer;
            inicio, fim: integer;
        end;
   function vazia (x: pilha):boolean;
   begin
     if x.cont = 0 then
        vazia := true
          else
            vazia := false;
   end;
   procedure criar (var x: pilha);
        begin
          x.cont := 0;
        end;
   procedure push(var x: pilha; y: integer);
         begin
            x.cont := x.cont + 1;
            x.obj[x.cont] := y ;
         end;
   function pop (var x: pilha): integer;
   begin
       pop := x.obj[x.cont];
       x.cont := x.cont - 1;
   end;
   function fcheia (f: fila): boolean;
   begin
     if f.fim > max then
      fcheia := true
        else 
           fcheia := false;
   end;
   procedure fcriar (var f: fila);
   begin
       f.inicio := 1;
       f.fim := 1;
   end;
   function fvazia (f: fila): boolean;
   begin
     if f.inicio = f.fim then
         fvazia := true
           else
              fvazia := false;
   end;    
   procedure enqueue (var f: fila; w: integer);
   begin
      if fcheia (f) then
          writeln ('fila cheia')
          else
            begin
             f.dados[f.fim] := w;
             f.fim := f.fim + 1;
            end;
   end;
   procedure dequeue (var f: fila);
   begin
      if fvazia(f) then
         writeln ('fila vazia')
         else
            begin
                writeln (f.dados[f.inicio]);
                f.inicio := f.inicio + 1;
            end;
   end;
var
  p: pilha;
  num, res: integer;
  f: fila;
 Begin
    criar(p);
    repeat 
       write ('digite um número ou "0" para sair: ');
 readln (num);
      if num = 0 then
      writeln ('saindo do programa')
      else
 push (p, num);
    until (num = 0); 
    fcriar (f);
    while not vazia(p) do
     begin
        res:= pop(p);
       enqueue (f, res);
     end;
     writeln ('imprimindo a pilha');
     while not fvazia(f) do
        dequeue (f);
       readln;
 End.
Gostou? Compartilhe!

0 comentários:

Seu Sistema Operacional é...

TEMAS

Visualizações