program stack;
uses crt;
const maxstack=15;
type tipestack=record
data:array[1..maxstack] of char;
top : integer;
end;
var S,T:tipestack;
x:char;i:integer;
k:string;
procedure inisialisasi(S:tipestack);
begin
S.top:=0;
end;
function penuh(S:tipestack):boolean;
begin
if S.top=maxstack then penuh:=true else penuh:=false;
end;
function kosong(S:tipestack):boolean;
begin
if S.top=0 then kosong:=true else kosong:= false;
end;
procedure push(var S:tipestack; x:char);
begin
if (penuh(S)=false) then
begin
S.top:=S.top+1;
S.data[S.top]:=x;
end
else
writeln('stack penuh');
end;
Procedure pop(var S:tipestack; var x:char);
begin
if (kosong(S)=false) then
begin
x:=S.data[S.top];
S.top:=S.top-1;
end
else
writeln('stack kosong');
end;
Procedure cetak (S:tipestack);
var i:integer;
begin
if kosong(S)=false then begin
for i:= 1 to S.top do
writeln(i,' ',S.data[i]);
end else
writeln('data kosong');
end;
Begin
clrscr;
writeln('masukkan kata :');
readln(k);
inisialisasi(S);
for i:=1 to length(k) do
push(s,k[i]);
cetak(S);
writeln;
writeln('data di balik');
inisialisasi(T);
for i:=S.top downto 1 do
begin
pop(S,x);
push(T,x);
end;
cetak(T);
readln;
end.
No comments:
Post a Comment