Senin, 20 Agustus 2012

Program dengan STACK

 Program Stack dengan List ( tipe data character )

Program StackdglinkedList;
uses wincrt;

type
PLinkedList = ^TlinkedList;
TLinkedlist = record
info : char;
next : PlinkedList;
end;

{menambah elemen baru pada stack}
Procedure Push(Var s:Plinkedlist; x:char);
var q : Plinkedlist;
Begin
new(q);
q^.info:=x;
q^.next:=s;
s:=q;
End;

function Pop(Var s :PlinkedList):char;
var t : PlinkedList;
Begin
if (s<>nil) then begin
t:=s;
pop:=s^.info;
s:=s^.next;
dispose(t);
end;
End;

Procedure Display(s:PlinkedList);
Begin
while s<> nil do begin
write(s^.info:5); s:=s^.next;
end;
writeln;
End;
{Program utama}
var a :Plinkedlist; y:char;
Begin
A:= nil;
push(A,'p');Push(A,'a');Push(A,'r');Push(A,'t');
Display(A);
REadln;
y:=Pop(A);
writeln('Hasil pop:',y);
readln;
y:=Pop(A);
writeln('Hasil pop:',y);
readln;
y:=Pop(A);
writeln('Hasil pop:',y);
readln;
y:=pop(A);
writeln('Hasil pop:',y);
readln;
End.
Output :t r a p
Hasil pop : t
Hasil pop : r
Hasil pop : a
Hasil pop : p

Tidak ada komentar:

Posting Komentar