Exercício de pilha e fila em Pascal (Dev-Pascal)
Autor: Amauri | Publicado: 2011-10-20T23:36:00-03:00

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Program filapilha ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Autor: Cícero Amauri&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp;Escreva um algoritmo que converta uma pilha implementada em: ·&nbsp; &nbsp; &nbsp; &nbsp; Fila, Considerar a lista com no máximo 100 elementos}&nbsp;
const max = 100;type&nbsp;&nbsp; &nbsp; pilha = record&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj: array [1..max] of integer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont: integer;&nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fila = record&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dados : array [1..max] of integer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inicio, fim: integer;&nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp;function vazia (x: pilha):boolean;&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp;if x.cont = 0 then&nbsp; &nbsp; &nbsp; &nbsp; vazia := true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vazia := false;&nbsp; &nbsp;end;&nbsp; &nbsp;procedure criar (var x: pilha);&nbsp; &nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.cont := 0;&nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp;procedure push(var x: pilha; y: integer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.cont := x.cont + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.obj[x.cont] := y ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;&nbsp; &nbsp;function pop (var x: pilha): integer;&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; &nbsp;pop := x.obj[x.cont];&nbsp; &nbsp; &nbsp; &nbsp;x.cont := x.cont - 1;&nbsp; &nbsp;end;&nbsp; &nbsp;function fcheia (f: fila): boolean;&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp;if f.fim &gt; max then&nbsp; &nbsp; &nbsp; fcheia := true&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fcheia := false;&nbsp; &nbsp;end;&nbsp; &nbsp;procedure fcriar (var f: fila);&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; &nbsp;f.inicio := 1;&nbsp; &nbsp; &nbsp; &nbsp;f.fim := 1;&nbsp; &nbsp;end;&nbsp; &nbsp;function fvazia (f: fila): boolean;&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp;if f.inicio = f.fim then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fvazia := true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fvazia := false;&nbsp; &nbsp;end; &nbsp; &nbsp;&nbsp; &nbsp;procedure enqueue (var f: fila; w: integer);&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; if fcheia (f) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeln ('fila cheia')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f.dados[f.fim] := w;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f.fim := f.fim + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp;end;&nbsp; &nbsp;procedure dequeue (var f: fila);&nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; if fvazia(f) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writeln ('fila vazia')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeln (f.dados[f.inicio]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.inicio := f.inicio + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp;end;var&nbsp; p: pilha;&nbsp; num, res: integer;&nbsp; f: fila;&nbsp;Begin&nbsp; &nbsp; criar(p);&nbsp; &nbsp; repeat&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;write ('digite um número ou "0" para sair: ');  &nbsp;readln (num);&nbsp; &nbsp; &nbsp; if num = 0 then&nbsp; &nbsp; &nbsp; writeln ('saindo do programa')&nbsp; &nbsp; &nbsp; else  &nbsp;push (p, num);&nbsp; &nbsp; until (num = 0);&nbsp;&nbsp; &nbsp; fcriar (f);&nbsp; &nbsp; while not vazia(p) do&nbsp; &nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; &nbsp; res:= pop(p);&nbsp; &nbsp; &nbsp; &nbsp;enqueue (f, res);&nbsp; &nbsp; &nbsp;end;&nbsp; &nbsp; &nbsp;writeln ('imprimindo a pilha');&nbsp; &nbsp; &nbsp;while not fvazia(f) do&nbsp; &nbsp; &nbsp; &nbsp; dequeue (f);&nbsp; &nbsp; &nbsp; &nbsp;readln;&nbsp;End.