Home

Program PointerPraktik2;

type
pointer = ^typedata;
typedata = record
nilai : integer ;
berikutnya : pointer;
end;

var list : pointer;

procedure masuk_depan(var head : pointer ; x : integer);
var
baru : pointer ;
begin
new(baru);
baru^.nilai := x;
baru^.berikutnya := nil;
if head = nil then head := baru
else
begin
baru^.berikutnya := head;
head := baru;
end;
end;

Procedure masuk_belakang (var head : pointer ; x : integer);
var
baru,bantu : pointer;
begin
new(baru);
baru^.nilai := x;
baru^.berikutnya:= nil;
bantu := head;
while bantu^.berikutnya <> nil do bantu:= bantu^.berikutnya;

bantu^.berikutnya:= baru;
end;

procedure hapus_depan(var head : pointer);
var
hapus : pointer ;
begin
hapus := head;
if head = nil then writeln (‘ List Kosong…’)
else
begin
head := head^.berikutnya;
dispose(hapus);
end;
end;

Procedure hapus_tengah(var head : pointer ; x : integer );
var
bantu, hapus : pointer ;
begin
bantu := head ;
if head = nil then writeln (‘ List kosong …’)
else
begin
bantu := head ;
while bantu^.berikutnya <> nil do
begin
if bantu^.berikutnya^.nilai = x then
begin
hapus := bantu^.berikutnya;
bantu^.berikutnya := hapus^.berikutnya;
dispose(hapus);
end
else
bantu := bantu^.berikutnya;
end;
end;
end;

Procedure hapus_belakang(var head : pointer );
var
hapus , bantu : pointer ;
begin
bantu := head;
if bantu = nil then writeln (‘List Kosong…’)
else
begin
while bantu^.berikutnya^.berikutnya <> nil do
bantu := bantu^.berikutnya;

hapus:= bantu^.berikutnya;
bantu^.berikutnya := nil ;
dispose(hapus);
end;
end;

Procedure cetak (l : pointer );
var
bantu : pointer ;
begin
bantu := l;
while bantu <> nil do
begin
write(bantu^.nilai :3);
bantu := bantu^.berikutnya;
end;
end;

Var
bil, bil1 : integer ;
jb : char ;

begin
new(list);
list := nil ;
jb := ‘y’;

writeln(‘Masukkan Depan ‘);
while UpCase (jb)=’y’ do
begin
write (‘Masukkan Bilangan : ‘);
readln (bil);
masuk_depan (list,bil);
write(‘Lagi [Y/T]’);
readln (jb);
end;

cetak (list);
writeln;

jb := ‘Y’;

writeln (‘ Masukkan Belakang ‘);
while UpCase (jb)=’Y’ do
begin
write (‘masukkan bilangan : ‘);
readln (bil);
masuk_belakang (list,bil);
write (‘Lagi [Y/T] ‘);
readln(jb);
end;
cetak(list); writeln; jb := ‘Y’;

writeln (‘Masuk Depan’);
while UpCase (jb) =’Y’ do
begin
write(‘Masukkan Bilangan : ‘);
readln(bil);
masuk_depan (list,bil);
write(‘Lagi [Y/T] ‘);
readln(jb);
end;
cetak(list);

writeln (‘HAPUS DEPAN ‘); hapus_depan(list) ; cetak(list); writeln;
writeln (‘JAPUS BELAKANG ‘) ; hapus_belakang(list) ; cetak(list) ; writeln ;

writeln (‘HAPUS TENGAH ‘);
write (‘Masukkan Bilangan Yang akan Dihapus : ‘); readln (bil );
hapus_tengah (list,bil) ; cetak (list); writeln;
readln;
readln;
end.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s