Skip to content

Instantly share code, notes, and snippets.

@olegon
Last active September 15, 2016 01:55
Show Gist options
  • Save olegon/e226cd14eaef13b30a406bb218027eb1 to your computer and use it in GitHub Desktop.
Save olegon/e226cd14eaef13b30a406bb218027eb1 to your computer and use it in GitHub Desktop.
{
São Paulo, 14 de setembro de 2016.
FATEC-SP
Pilhas em Pascal
}
program prog_pilha;
const tamanho_da_pilha = 5;
type t_pilha = integer;
type pilha = record
vetor: array[1..tamanho_da_pilha] of t_pilha;
topo: integer;
end;
procedure init (var p : pilha);
begin
p.topo := 0;
end;
function isempty (p : pilha) : boolean;
begin
isempty := p.topo = 0;
end;
function isfull (p : pilha) : boolean;
begin
isfull := p.topo = tamanho_da_pilha;
end;
procedure push (var p : pilha; item : t_pilha);
begin
inc(p.topo);
p.vetor[p.topo] := item;
end;
function pop (var p: pilha) : t_pilha;
begin
pop := p.vetor[p.topo];
dec(p.topo);
end;
function indiceDoMaiorElemento (p : pilha) : integer;
var maiorElemento, topo : t_pilha;
begin
if isempty(p) then
{ -1 se a pilha estiver vazia. }
indiceDoMaiorElemento := -1
else
begin
maiorElemento := pop(p);
indiceDoMaiorElemento := p.topo + 1;
while not isempty(p) do
begin
topo := pop(p);
if topo > maiorElemento then
indiceDoMaiorElemento := p.topo + 1;
end;
end;
end;
var p : pilha;
var item : t_pilha;
begin
init(p);
while not isfull(p) do
begin
readln(item);
push(p, item);
end;
writeln('Índice do maior elemento: ', indiceDoMaiorElemento(p));
writeln('Tamanho da pilha: ', p.topo); // Exibe $tamanho_da_pilha O_O
{
while not isempty(p) do
begin
writeln(pop(p));
end;
}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment