Jumat, 24 Desember 2010

Program Stack dengan Linked List dalam Pascal

Contoh Listing Program dalam Pascal
{Algoritma dan struktur data}
Baca juga sumber :
http://en.wikipedia.org/wiki/Stack_(data_structure)


Program dengan STACK
1. 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

2. Program Stack dengan LinkedList ( tipe integer )

Program StackdglinkedList;
uses wincrt;
type
PLinkedList = ^TlinkedList;
TLinkedlist = record
info : integer;
next : PlinkedList;
end;


Procedure Push(Var s:Plinkedlist; x:integer);
var q : Plinkedlist;
Begin
new(q);q^.info:=x;q^.next:=s;s:=q;
End;

function Pop(Var s :PlinkedList):integer;
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:integer;
Begin
A:= nil;
push(A,7);Push(A,6);Push(A,1);
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;
End.
Output : 1 6 7
Hasil pop : 1
Hasil pop : 6
Hasil pop : 7

3.Program stack
Program StackdglinkedList;
uses wincrt;

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


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

function Pop(Var s :PlinkedList):integer;
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:integer;
Begin
A:= nil;
push(A,2);Push(A,1);Push(A,7);Push(A,9);
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 : 9 7 1 2
Hasil pop : 9
Hasil pop : 7
Hasil pop : 1
Hasil pop : 2

Tidak ada komentar: