Sabtu, 23 Mei 2015

menara hanoi dengan stack versi 1

program stackhanoi;
uses crt;

Const
         X = 3;
         Z  = 0;
Type
         jenis = integer;
         Stack = record
              posisi : array[1..X] of jenis;
              Top : Z..X;
End;

var
A,B,C:stack;
langkah,j:integer;


function nol(s:stack):boolean;
begin
if s.top=0 then nol:=true else nol:=false;
end;

Procedure Push (var S : stack; data: jenis);
Begin
              S.Top := S.Top + 1;
              S.posisi[S.Top]:= data;
End;

function POP (var S : stack):jenis;
Begin
              pop := S.posisi[S.Top];
              S.Top := S.Top - 1;
End;

procedure output (var s:stack);
var i:integer;
begin
if nol(s) then write ('') else
 begin
 for i:=1 to s.top do
 write(s.posisi[i],' ');
 end;
 writeln;
end;

begin

push(A,3); push(A,2); push(A,1);

for j:=0 to 7 do begin
clrscr;
 langkah:=j;
case langkah of
 1: begin
    pop(A); push(C,1);
    end;
 2: begin
    pop(A); push(B,2);
    end;
 3: begin
    pop(C); push(B,1);
    end;
 4: begin
    pop(A); push(C,3);
    end;
 5: begin
    pop(B); push(A,1);
    end;
 6: begin
    pop(B); push(C,2);
    end;
 7: begin
    pop(A); push(C,1);
    end;
 end;

write('Tower A : '); output(A);
write('Tower B : '); output(B);
write('Tower C : '); output(C);
readln;
 end;
end.

Tidak ada komentar:

Posting Komentar