Modul Struktur Data Genap 2011.v2.docx

1
Binary
File: 06hapus.pas

Modul Struktur Data
Daftar Isi
Contents
Tipe Data............................................................................................................... 3
maxint.pas.......................................................................................................... 3
maxint02.pas...................................................................................................... 3
tipedata.pas....................................................................................................... 5
Text File................................................................................................................. 7
TEXT01.pas......................................................................................................... 7
TEXT02.pas......................................................................................................... 7
TEXT03.pas......................................................................................................... 8
TEXT04.pas......................................................................................................... 9
Binary File............................................................................................................ 11
01dbw.pas........................................................................................................ 11
02dbread.pas................................................................................................... 12
03dbw.pas........................................................................................................ 12
04dbread.pas................................................................................................... 14

05trunc.pas...................................................................................................... 15
06hapus.pas..................................................................................................... 16
07DB.pas.......................................................................................................... 17
FilePos.pas........................................................................................................ 17
Stack (Tumpukan)............................................................................................... 17
01balik.pas....................................................................................................... 17
if2pf.pas............................................................................................................ 17
tumpukan.pas................................................................................................... 17
random.pas...................................................................................................... 17
Queue (Antrian)................................................................................................... 17
antrian.pas....................................................................................................... 17
antrian2.pas..................................................................................................... 17
antrian3.pas..................................................................................................... 17
Linked List (Senarai Berantai)..............................................................................17
linkedls.pas....................................................................................................... 17
dll.pas............................................................................................................... 17

2
Binary
File: 06hapus.pas

Tree (Pohon Keluarga)......................................................................................... 17
tree.pas............................................................................................................ 17
Sort (Pengurutan)................................................................................................ 17
bubble.pas........................................................................................................ 17
insort.pas.......................................................................................................... 17
qsort.pas........................................................................................................... 17

3
Binary
File: 06hapus.pas

Tipe Data
maxint.pas
1
2
3
4
5
6
7

8
9
10
11
12
13
14
15
16
17
18
19
20

{
Type

Range

Format


Shortint
-128..127
Signed 8-bit
Integer -32768..32767
Signed 16-bit
Longint -2147483648..2147483647 Signed 32-bit
Byte
0..255 Unsigned 8-bit
Word
0..65535
Unsigned 16-bit

All Integer types are ordinal types.
}
uses wincrt;
begin
writeln('nilai tertinggi untuk integer adalah: ', maxint);
writeln('nilai tertinggi untuk long integer adalah: ',
maxlongint);

end.

21
22
Tuliskan tampilan kode di atas

maxint02.pas
1 uses wincrt;
2 var
i:integer;
3
j:longint;
4
5
6 begin
i:=maxint;
7
writeln('nilai tertinggi untuk integer adalah: ', i);
8
9

j:=maxlongint;
10
writeln('nilai tertinggi untuk long integer adalah: ', j);
11
12
writeln;
13
14
i:=i+1;
15

4
Binary
File: 06hapus.pas
writeln('nilai sebuah variabel integer apabila

16 ',maxint,'+1=',i);
j:=j+1;
17


writeln('nilai sebuah variabel longint apabila

18 ',maxlongint,'+1=',j);
19
writeln;
20
21
writeln('nilai maxint +1 adalah: ', maxint+1);
22
writeln('nilai maxlongint +1 adalah: ', maxlongint+1);
23
writeln('dapatkah kalian mengambil kesimpulan dari dua contoh di

24 atas?');
25
26 end.
Ketikkan kode di atas menggunakan Turbo Pascal 7.0 for DOS dengan mengubah
baris #1 menjadi uses CRT; dan tuliskan error saat mengcompile nya, dan pada
baris berapa?


Lengkapi tabel berikut:

Error
#

Error Message

1

Invalid function number

2

File not found

3

Path not found

4


Too many open fles

5

File access denied

6

Invalid fle handle

12

Invalid fle access code

15

Invalid drive number

16


Cannot remove current directory

17

Cannot rename across drives

100

Disk read error

101

Disk write error

102

File not assigned

103


File not open

104

File not open for input

105

File not open for output

106

Invalid numeric format

200

Division by zero

201

Range check error

202

Stack overflow error

203

Heap overflow error

204

Invalid pointer operation

205

Floating point overflow

5
Binary
File: 06hapus.pas
206 Floating point underflow
207 Invalid floating point operation
210

Object not initialized

211

Call to abstract method

212

Stream registration error

213

Collection index out of range

214

Collection overflow error

Buka kembali source-code pascal di atas dengan menggunakan TPW, lalu
jalankan. Tuliskan hasilnya!

tipedata.pas
1 {
2
3 praktikum tipe data:
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

pengenalan tipe data bentukan (tipe data yang dibentuk dari tipe
data muasal)

Muhammad Jazman
Wednesday, December 05, 2007 1:43:36 PM
}
uses wincrt;

const
pi = 3.14;
nmaks = 100;
sandi = 'xyz';

type
mahasiswa
NIM
:
nama :
usia :
end;

= record
longint;
string;
byte;

koordinat = record

6
Binary
File: 06hapus.pas
x,y:real;
30
end;
31
32
jam = record
33
hh,mm,ss:byte;
34
end;
35
36
37 var
p:jam;
38
39
40 begin
p.hh:= 23;
41
p.mm:= 59;
42
p.ss:= 59;
43
44
writeln('jam menunjukkan pukul ',p.hh,':',p.mm,':',p.ss);
45
46 end.

Jika variable x bertipe byte.Nilai x awalnya 250 kemudian ditambahkan 50.
Berapakah nilai x sekarang? Mengapa? Demonstrasikan di depan instruktur!

Lengkapi tabel berikut:

Type

Range

Digits

Bytes

real

2.9e-39..1.7e38

11-12

6

single

1.5e-45..3.4e38

7-8

4

double

5.0e-324..1.7e308

15-16

8

extended

3.4e4932..1.1e4932

19-20

10

comp

-9.2e18..9.2e18

19-20

8

7
Binary
File: 06hapus.pas

Text File
TEXT01.pas
1 uses wincrt;
2 var
F : text;
3
s : string;
4
5 begin
clrscr;
6
write('Input file name to read : '); readln(s);
7
assign(F,s);
{ associate it }
8
reset(F);
{ open it
}
9
while not EOF(F) do

{ read it until it's done

10 }
begin
11
readln(F,s);
12
writeln(s);{tampilkan di layar}
13
end;
14
close(F);
{ close it }
15
16 end.
Demonstrasikan di depan instruktur Anda hasil menjalankan program di atas.
Tuliskan pesan kesalahan saat fle yang akan dibaca ternyata tidak ada!

Apa maksud perintah readln(s); pada baris #7?

Apa maksud perintah readln(F,s); pada baris #12?

TEXT02.pas
1
2
3
4
5
6
7
8
9
10
11
12
13

{
demo read text file line by line
}
uses wincrt;
var
F : text;
s : string;
i : integer;
begin
clrscr;
write('Input file name to read : '); readln(s);
assign(F,s);
{ associate it }
reset(F);
{ open it
}

8
Binary
File: 06hapus.pas
while not EOF(F) do
{ read it until it's done }
14
begin
15
i:=i+1;
16
readln(F,s);
17
writeln(i:3,' ':2,s);{tampilkan di layar}
18
end;
19
close(F);
{ close it }
20
21 end.
Apa maksud i:3 pada baris #18?

Apa maksud ' ':2 pada baris #18?

TEXT03.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

{ ERROR HANDLING ON OPENING A FILE }
uses wincrt;
var
F : text;
s : string;
n : integer;
begin
clrscr;
write('Input file name to read : '); readln(s);
assign(F,s);
{ associate it }
{reset(F);
open it
}
{$I-}
reset(F);
{$I+}
n:=IOResult;
if n0 then
begin
writeln('Error encountered in reading file ',s);
case n of
2: writeln('File not found');
3: writeln('Path not found');
4: writeln('Too many open files');
5: writeln('File access denied');
100: writeln('Disk read error');
101: writeln('Disk write error');
150: writeln('Disk is write protected');
152: writeln('Drive is not ready');
154: writeln('CRC error in data');
156: writeln('Disk seek error');

9
Binary
File: 06hapus.pas
157: writeln('Unknown media type');
31
162: writeln('Hardware failure');
32
else writeln('Various error');
33
end;
34
halt;
35
end;
36
37
38
39
while not EOF(F) do
{ read it until it's done }
40
begin
41
readln(F,s);
42
writeln(s);{tampilkan di layar}
43
end;
44
close(F);
{ close it }
45
46 end.
Apa maksud perintah {$I-} pada baris #13?
Apa maksud perintah

{$I+} pada baris #15?

Apa maksud perintah

n:=IOResult; pada baris #16?

Demonstrasikan ke instruktur Anda program di atas. Kemudian, ubahlah dengan
menambahkan blok repeat-until, sehingga logika programnya menjadi akan
terus menanyakan nama fle yang akan dibaca jika masih terjadi kesalahan. Hint:
gunakan nilai variable n untuk mengetahui jika masih terjadi kesalahan.

TEXT04.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

{
write a text file
}
uses wincrt;
var
F : text;
s : string;
begin
clrscr;
write('Input file name to create : '); readln(s);
assign(F,s);
{ associate it }
rewrite(F);
{ create it
}
writeln('Just enter any text and followed by enter.');
writeln('To quit : Input an empty line.');

10 File: 06hapus.pas
Binary
16
repeat
17
readln(s);
{ read a line }
18
{if s='' then break;} {if s is an empty line --> break!}
19
writeln(F,s);
20
until s='';
21
22
close(F);
{ close it }
23
24 end.

WARNING: JANGAN DIJALANKAN DAHULU JIKA ANDA TIDAK MENGERTI BARISDEMI-BARIS PERINTAH DI ATAS, KARENA AKAN MENIMPA BERKAS ANDA YANG
LAMA

Tuliskan maksud perintah readln(s); pada baris #10!

Apa beda perintah readln(s); di baris #10 di atas dengan readln(F,s); baris
#42 di TEXT03.pas?

Apa maksud perintah assign(F,s); pada baris #11?

Apa maksud perintah rewrite(F); pada baris #12?

Apa maksud perintah writeln(F,s); pada baris #20?

Jelaskan logika program baris #17 – #21!

11 File: 06hapus.pas
Binary

Binary File
01dbw.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

{ A crude database recording }
uses crt;
type
Temployee = record
name
: string[20];
address : string[40];
phone
: string[15];
age
: byte;
salary : longint;
end;
var
F
c
r
s

:
:
:
:

file of Temployee;
char;
Temployee;
string;

begin
clrscr;
write('Input file name to record databases : '); readln(s);
assign(F,s);
rewrite(F);
repeat
clrscr;
write('Name
write('Address
write('Phone
write('Age
write('Salary
write(F,r);

{ Associate it }
{ Create it
}

=
=
=
=
=

');
');
');
');
');

readln(r.name);
readln(r.address);
readln(r.phone);
readln(r.age);
readln(r.salary);

{ Input data }

{ Write data to file }

write('Input data again (Y/N) ?');
repeat
c:=upcase(readkey);
{ Ask user : Input again or not }
until c in ['Y','N'];
writeln(c);
until c='N';
close(F);

12 File: 06hapus.pas
Binary
43 end.
Apa maksud perintah F : file of Temployee; pada baris #13?
Apa maksud perintah

readln(r.name);

Apa maksud perintah

write(F,r); pada baris #33?

pada baris #27?

Apa maksud blok perintah repeat-until baris #36 – #38?
Apa maksud blok perintah repeat-until baris #25 – #40?
Demokan program diatas, gunakan/timpa fle test01.emp untuk fle penyimpan
data kita, lalu gunakan program 02dbread.pas dibawah ini untuk membacanya
kembali.

02dbread.pas
{ A crude database read }
uses crt;
type
Temployee = record
name
address
phone
age
salary
end;
var

F
c
r
s

:
:
:
:

:
:
:
:
:

string[20];
string[40];
string[15];
byte;
longint;

file of Temployee;
char;
Temployee;
string;

begin
write('Input file name to read databases : '); readln(s);
assign(F,s);
reset(F);

{ Associate it }
{ reset pointer ke awal file, open it}

clrscr;
while not EOF(F) do begin
read(F,r);
writeln('Name
= ',r.name);
writeln('Address = ',r.address);
writeln('Phone
= ',r.phone);
writeln('Age
= ',r.age);
writeln('Salary = ',r.salary);
writeln;
end;
close(F);
end.

03dbw.pas
1 { A crude database recording

13 File: 06hapus.pas
Binary
2
now with ability to detect if input file is not exist
3
new features:
4
* APPENDING
5
* IO Check
6 }
7 uses crt;
8 type
9
Temployee = record
10
name
: string[20];
11
address : string[40];
12
phone
: string[15];
13
age
: byte;
14
salary : longint;
15
end;
16
17 var
18
F : file of Temployee;
19
c : char;
20
r : Temployee;
21
s : string;
22
n : integer;
23
24 begin
25
write('Input file name to record databases : '); readln(s);
26
27
assign(F,s);
{ Associate it }
28
29
{$I-}
30
reset(F); { Open it. COMPARE THIS ROUTINE WITH 01DBW.PAS }
31
{$I+}
32
33
n:=IOResult;
34
if n0 then
{ If it's doesn't exist then }
35
begin
36
{$I-}
37
rewrite(F);
{ Create it
}
38
{$I+}
39
n:=IOResult;
40
if n0 then
41
begin
42
writeln('Error creating file !'); halt;
43
end;
44
end
45
else
{ If it exists then }
46
seek(F,filesize(F)); { Move file pointer to the last record }
47
{compare it with reset(F)}
48
49
repeat

14 File: 06hapus.pas
Binary
50
clrscr;
51
write('Name
= '); readln(r.name);
{ Input data }
52
write('Address = '); readln(r.address);
53
write('Phone
= '); readln(r.phone);
54
write('Age
= '); readln(r.age);
55
write('Salary = '); readln(r.salary);
56
57
write(F,r);
{ Write data to file }
58
59
write('Input data again (Y/N) ?');
60
repeat
61
c:=upcase(readkey);
{ Ask user : Input again or not }
62
until c in ['Y','N'];
63
writeln(c);
64
until c='N';
65
66
close(F);
67 end.
Apa maksud blok perintah #29 – #31?
Apa maksud blok perintah #36 – #38?
Apa maksud perintah seek(F,filesize(F)); pada baris #46?
Apa maksud blok perintah #49 – #64?

04dbread.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

{ A crude database read with data number}
uses crt;
type
Temployee = record
name
: string[20];
address : string[40];
phone
: string[15];
age
: byte;
salary : longint;
end;
var
F
c
r
s
i

:
:
:
:
:

file of Temployee;
char;
Temployee;
string;
integer;

begin
write('Input file name to read databases : '); readln(s);

15 File: 06hapus.pas
Binary
21
assign(F,s); { Associate it }
22
reset(F);
{ reset pointer ke awal file}
23
24
clrscr;
25
for i:=0 to filesize(F)-1 do
26
begin
27
read(F,r);
28
writeln;
29
writeln('DATA ',i+1);
30
writeln('Name
= ',r.name);
31
writeln('Address = ',r.address);
32
writeln('Phone
= ',r.phone);
33
writeln('Age
= ',r.age);
34
writeln('Salary = ',r.salary);
35
end;
36
37
close(F);
38
39 end.
Apa maksud blok program #26 – #36?
Tambahkan kode di atas sehingga apabila flenya tidak ada, user akan tetap
ditanya nama fle yang akan dibuka.

05trunc.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

uses Crt;
var
f: file of Integer;
i,j: Integer;
begin
Assign(f,'TEST.INT');
Rewrite(f);
for i := 1 to 6 do
Write(f,i);
Writeln('File before truncation:');
Reset(f);
while not Eof(f) do
begin
Read(f,i);
Writeln(i);
end;
Reset(f);
for i := 1 to 3 do
Read(f,j); { Read ahead 3 records }
Truncate(f); { Cut file off here }

16 File: 06hapus.pas
Binary
Writeln;
24
Writeln('File after truncation:');
25
Reset(f);
26
while not Eof(f) do
27
begin
28
Read(f,i);
29
Writeln(i);
30
end;
31
32
Close(f);
33
Erase(f);
34
end.
35
Apa maksud perintah Assign(f,'TEST.INT'); pada baris #7?
Apa maksud perintah Rewrite(f); pada baris #8?
Apa maksud blok program baris #9 – #10?

Apa maksud blok program baris #14 – #18?

Apa maksud perintah Truncate(f); pada baris #23?

Jelaskan secara singkat maksud program di atas secara keseluruhan!

06hapus.pas
uses Crt;
1
2
3 var
f: file of Integer;
4
i,j: Integer;
5
6 begin
Assign(f,'TEST.INT');
7
Rewrite(f);
8
for i := 1 to 6 do
9
Write(f,i);
10
11
12
Writeln('File before truncation:');
13
Reset(f);
14
while not Eof(f) do
15
begin
16
Read(f,i);
17
Writeln(i);
18

17 File: 06hapus.pas
Binary
end;
19
20
21
Reset(f);
22
for i := 1 to 3 do
23
Read(f,j); { Read ahead 3 records }
24
Truncate(f); { Cut file off here }
25
Writeln;
26
Writeln('File after truncation:');
27
Reset(f);
28
while not Eof(f) do
29
begin
30
Read(f,i);
31
Writeln(i);
32
end;
33
34
35
Close(f);
36
Erase(f);
37
end.
38

07DB.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

{
program untuk administrasi database employee
Muhammad Jazman
Tuesday, December 04, 2007
1:04:54 PM
}
uses crt;

type
Temployee
name
address
phone
age
salary
end;

=
:
:
:
:
:

record
string[20];
string[40];
string[15];
byte;
longint;

var
F : file of Temployee;
r : Temployee;{penunjuk untuk filenya}
s : string;{nama file}

18 File: 06hapus.pas
Binary
i,j : integer;
24
n : integer; {untuk menampung IOResult}
25
26
27
28
29 procedure showAll;
30 begin
clrscr;
31
reset(f);
32
for i:=1 to filesize(F) do
33
begin
34
read(F,r);
35
writeln;
36
writeln('DATA ',i);
37
writeln('Name
= ',r.name);
38
writeln('Address = ',r.address);
39
writeln('Phone
= ',r.phone);
40
writeln('Age
= ',r.age);
41
writeln('Salary = ',r.salary);
42
end;
43
44 end;
45
46
47 procedure showRange(mulai:integer; akhir:integer);
48 begin
clrscr;
49
seek(f,mulai-1);
50
for i:=mulai to akhir do
51
begin
52
read(F,r);
53
writeln;
54
writeln('DATA ',i);
55
writeln('Name
= ',r.name);
56
writeln('Address = ',r.address);
57
writeln('Phone
= ',r.phone);
58
writeln('Age
= ',r.age);
59
writeln('Salary = ',r.salary);
60
end;
61
62 end;
63
64 procedure showRecord(index:integer);
65 begin
clrscr;
66
seek(f,index-1);
67
read(F,r);
68
writeln;
69
writeln('DATA ',i);
70
writeln('Name
= ',r.name);
71

19 File: 06hapus.pas
Binary
writeln('Address = ',r.address);
72
writeln('Phone
= ',r.phone);
73
writeln('Age
= ',r.age);
74
writeln('Salary = ',r.salary);
75
76 end;
77
78 procedure delRecord(index:integer);
79 begin
for i:=index to fileSize(f)-1 do
80
begin
81
seek(f,i);
82
read(f,r);{read data sesudahnya}
83
seek(f,i-1);{balik lagi ke data sebelumnya}
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

write(f,r);{timpa data yang lama dengan data record
berikutnya}
end;
seek(f,filesize(f)-1);
truncate(f);{buang yang terakhir}
end;

begin
write('Input file name to read databases : '); readln(s);
assign(F,s);{ Associate it }
{$I-}
reset(F);{ reset pointer ke awal file}
{$I+}
n:=IOResult;
if n0 then { If it's doesn't exist then }
begin
{$I-}
rewrite(F);
{ Create it
}
{$I+}
n:=IOResult;
if n0 then
begin
writeln('Error creating file !');
halt;
end;
end
else{ If it exists then }
seek(F,filesize(F)); { Move file pointer to the last record }

showAll;
{readln;}
{

20 File: 06hapus.pas
Binary
showRange(2,3);
119
readln;
120
121 }
122 {
showRecord(2);
123
124 }
125 {
delrecord(2);
126
showAll;
127
readln;
128
129 }
130 close(F);
131
132
133
134 end.

FilePos.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

uses WinCrt;
var
f: file of Byte;
size : Longint;
begin
{ Get file name from command line }
Assign(f, ParamStr(1));
Reset(f);
size := FileSize(f);
Writeln('File size in bytes: ',size);
Writeln('Seeking halfway into file...');
Seek(f,size div 2);
Writeln('Position is now ',FilePos(f));
Close(f);
end.

21 File: 06hapus.pas
Binary

Stack (Tumpukan)
01balik.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

program BALIK_KALIMAT;
uses wincrt;
const elemen = 255; {batas maksimum karakter}
type
myString = string[elemen];
stack = record
isi:myString;
atas:0..elemen
end;
var
t:stack;
i:integer;
kalimat:myString;

procedure awalan (var t:stack);
begin
{inisialisasi stack, bahwa stack masih kosong}
t.atas := 0
end;

procedure push(var t:stack; x:char);
begin
t.atas := t.atas + 1;
t.isi[t.atas]:=x
end;

function pop(var t:stack):char;
begin
pop := t.isi[t.atas];
t.atas := t.atas - 1
end;
{program utama}
begin

22 File: 06hapus.pas
Binary
clrscr;
42
awalan(t);
43
writeln('Stack untuk membalik kalimat');
44
writeln;
45
write('Masukkan sembarang kalimat: ');
46
readln(kalimat);
47
{
48
clrscr;
49
}
50
writeln('Kalimat Asli: ');
51
writeln(kalimat);
52
writeln;writeln;
53
writeln('Setelah dibalik: ');
54
55
{push setiap karakternya ke stack kita}
56
for i:= 1 to length(kalimat) do
57
push(t,kalimat[i]);
58
59
60
{pop satu-satu}
61
for i:= 1 to length(kalimat) do
62
write(pop(t));
63
writeln
64
65 end.

if2pf.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

program KONVERSI_INFIX_KE_POSTFIX;
USES wincrt;
const
max_elemen = 255;
type
smax
= string[max_elemen];
tumpukan = record
rinci:smax;
atas: 0..max_elemen
end;
var
infix:smax; {notasi infix}
lagi:char;
i:integer;

function valensi(tanda_op:char):integer;
begin
case tanda_op of

23 File: 06hapus.pas
Binary
'^'
: valensi:= 3; {pangkat}
22
'*','/' : valensi:= 2; {kali,bagi}
23
'+','-' : valensi:= 1; {tambah,kurang}
24
'('
: valensi:= 0
25
end
26
27 end;
28
29
30
31 procedure push (var t:tumpukan; elemen:char);
32 begin
t.atas:= t.atas + 1;
33
t.rinci[t.atas]:= elemen;
34
35 end;
36
37
38
39 function pop (var t:tumpukan): char;
40 begin
pop:= t.rinci[t.atas];
41
t.atas := t.atas -1
42
43 end;
44
45
46
47 procedure infix2postfix (infix:smax);
48 var
i:integer;
49
operator:set of char;
50
temp,kar:char;
51
t:tumpukan;
52
test:boolean;
53
54 begin
operator:=['^']+['*']+['/']+['+']+['-'];
55
56
{melakukan konversi}
57
for i:= 1 to length(infix) do
58
begin
59
kar:= infix[i];
60
if kar= '(' then push(t,kar)
61
62
else if kar = ')' then
63
begin
64
while t.rinci[t.atas] '(' do
65
write(pop(t):2);{cetak}
66
temp:= pop(t)
67
end
68
else if kar in operator then
69

24 File: 06hapus.pas
Binary
begin
70
{selama stack dan valensi kar sekarang lebih kecil dari

71 yang ada di stack}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

while (t.atas 0) and (valensi(kar) baru^.berikut^.info do
71
bantu:=bantu^.berikut;
72
73
{menyisipkan simpul baru}
74
baru^.berikut:=bantu^.berikut;
75
bantu^.berikut:=baru;
76
end;
77
end;
78
79
80 {
program 5.4
81
prosedur menghapus simpul
82
83 }
84 procedure hapus_simpul(var awal,akhir:simpul;elemen:char);
var bantu,hapus:simpul;
85
begin
86
if awal=nil then {senarai masih kosong}
87
writeln('senarai masih kosong, tidak dapat menghapus')
88
else if awal^.info = elemen then
89
{simpul pertama dihapus}
90

47 File: 06hapus.pas
Binary
begin
91
hapus:=awal;
92
awal:=hapus^.berikut;
93
dispose(hapus)
94
end
95
else
96
{menghapus simpul tengah atau terakhir}
97
begin
98
bantu:=awal;
99
{mencari simpul yang akan dihapus}
100
while (elemenbantu^.info) and (bantu^.berikut nil) do
101
bantu:=bantu^.berikut;
102
103
hapus:=bantu^.berikut;
104
if hapusnil then
105
{simpul yang akan dihapus ketemu}
106
begin
107
if hapus akhir then
108
{simpul tengah dihapus}
109
bantu^.berikut:=hapus^.berikut
110
else
111
{simpul terakhir dihapus}
112
begin
113
akhir:=bantu;
114
akhir^.berikut:=nil
115
end;
116
dispose(hapus)
117
end
118
119
else
120
{simpul yang akan dihapus tidak ketemu}
121
writeln('Simpul yang akan dihapus tidak ada')
122
end
123
end;
124
125
126
127 {
program 5.5
128
prosedur untuk membaca senarai dari kiri ke kanan (maju)
129
130 }
131 procedure baca_maju (awal,akhir:simpul);
var
132
bantu:simpul;
133
begin
134
bantu:=awal;
135
repeat
136
write(bantu^.info:2);
137
bantu:=bantu^.berikut
138

48 File: 06hapus.pas
Binary
until bantu=nil;
139
writeln
140
end;
141
142
143
144 {
Program 5.6
145
Prosedur untuk membalik pointer
146
147 }
148 procedure balik_pointer(var awal, akhir: simpul);
var
149
bantu, bantu1: simpul;
150
begin
151
bantu:= awal;
152
awal:= akhir;
153
154
{proses membalik pointer}
155
repeat
156
bantu1 := bantu;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

{mencari simpul sebelum simpul yang ditunjuk oleh pointer

akhir}

while bantu1^.berikut akhir do
bantu1 := bantu1^.berikut;
akhir^.berikut:=bantu1;
akhir:= bantu1;
until akhir = bantu;
akhir^.berikut:= nil;
end;
{
program 5.7
prosedur membaca senarai dari kanan ke kiri
menggunakan cara pembacaan secara rekursif
}
procedure mundur (bantu:simpul);
begin
if bantu nil then
begin
mundur(bantu^.berikut);
write(bantu^.info:2)
end;
end;
{
program 5.8
mencari data pada senarai tak terurutkan

49 File: 06hapus.pas
Binary
jiwa fungsi bernilai 'true' berarti data ketemu
186
187 }
188 function ada_data (elemen:char; awal:simpul):boolean;
var
189
ketemu:boolean;
190
begin
191
ketemu:=false;
192
repeat
193
if awal^.info = elemen then
194
ketemu := true
195
else
196
awal := awal^.berikut;
197
until ketemu or (awal = nil);
198
ada_data := ketemu
199
end;
200
201
202 {
program 5.9
203
Fungsi untuk mencari data pada senarai terurutkan
204
Jika fungsi bernilai 'true' bearti data ketemu
205
206 }
207 function ada_data1 (awal:simpul; elemen:char):boolean;
208 var ketemu:boolean;
209 begin
ketemu:=false;
210
repeat
211
if awal^.info = elemen then
212
{data ditemukan}
213
ketemu := true
214
else
215
if awal^.info < elemen then
216
awal := awal^.berikut
217
else
218
{tidak ditemukan}
219
awal:=nil
220
until ketemu or (awal=nil);
221
222
ada_data1:=ketemu
223
224 end;
225
226 {
program 5.10
227
prosedur inisialisasi senarai berantai
228
untuk membentuk simpul kepala
229
230 }
231 procedure awalan(var awal:simpul);
232 begin
new(awal);
233

50 File: 06hapus.pas
Binary
{karena belum punya simpul, ini adalah simpul terakhir}
234
awal^.berikut := nil;
235
236 end;
237
238
239
240 {main program}
241 begin
clrscr;
242
writeln('Program untuk menguji Linked List');
243
writeln;
244
writeln('Nilai awal^.info:=',awal^.info);
245
writeln('Nilai akhir^.info:=',akhir^.info);
246
writeln;
247
248
awalan(awal);
249
writeln('awalan(awal);');
250
writeln('Nilai awal^.info:=',awal^.info);
251
writeln('Nilai akhir^.info:=',akhir^.info);
252
writeln;
253
254
tambah_belakang(awal,akhir,'a');
255
writeln('tambah_belakang(awal,akhir,''a'');');
256
writeln('Nilai awal^.info:=',awal^.info);
257
writeln('Nilai akhir^.info:=',akhir^.info);
258
writeln;
259
260
tambah_belakang(awal,akhir,'b');
261
tambah_belakang(awal,akhir,'c');
262
tambah_belakang(awal,akhir,'d');
263
tambah_belakang(awal,akhir,'e');
264
tambah_