-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pas
228 lines (192 loc) · 5.3 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
program SimpleStack;
{I.S : Memilih menu yang diinginkan}
{F.S : Menghasilkan menu yang dipilih}
uses
crt;
type
TNodePointer = ^TNode;
TNode = record
Item : integer;
Next : TNodePointer;
end;
var
Top: TNodePointer;
Option : integer;
function Empty(Top : TNodePointer) : boolean;
{I.S : Harga Top sudah di definisikan}
{F.S : Menghasilkan nilai benar atau salah}
begin
Empty := false;
if (Top = nil) then
Empty := true;
end;
function OneList(Top : TNodePointer) : boolean;
{I.S : Harga Top sudah di definisikan}
{F.S : Menghasilkan nilai benar atau salah}
begin
OneList := false;
if (Top^.Next = nil) then
OneList := true;
end;
procedure Initialize(var Top : TNodePointer);
{I.S : Harga Top sudah di definisikan}
{F.S : Menghasilkan Top dengan nilai Nil}
begin
Top := nil;
end;
procedure MainMenu;
{I.S : Parameter sembarang}
{F.S : Menghasilkan tampilan menu utama}
begin
write('+------------------------------------------------------------------------------+');
write('| | [1] Push | [2] Pop | [0] Exit | |');
write('+------------------------------------------------------------------------------+');
write('| | Choose one : | |');
write('+------------------------------------------------------------------------------+');
writeln;
end;
procedure PushMenu;
{I.S : Parameter sembarang}
{F.S : Menghasilkan tampilan menu push}
begin
write('+------------------------------------------------------------------------------+');
write('| | Please input number only | |');
write('+------------------------------------------------------------------------------+');
write('| | Item : | |');
write('+------------------------------------------------------------------------------+');
writeln;
end;
procedure PopHistory(Item : integer);
{I.S : Parameter sembarang}
{F.S : Menghasilkan tampilan history pop}
begin
write('+------------------------------------------------------------------------------+');
write('| Item Pop : ', Item);
gotoxy(80, 2);
write('|');
write('+------------------------------------------------------------------------------+');
writeln;
end;
procedure ShowList(Top : TNodePointer);
{I.S : Harga Top sudah didefinisikan}
{F.S : Menampilkan list}
var
Assist : TNodePointer;
begin
write('+------------------------------------------------------------------------------+');
if (Top = nil) then
begin
write(' | / | ');
write(' Top ');
end
else
begin
Assist := Top;
writeln('Top');
while (Assist <> nil) do
begin
write(Assist^.Item, ' -> ');
Assist := Assist^.Next;
end;
writeln('nil');
end;
write('+------------------------------------------------------------------------------+');
end;
procedure Push(var Top : TNodePointer; Item : integer);
{I.S : Harga Top dan Item sudah didefinisikan}
{F.S : Menambahkan item ke dalam list}
var
NewNode: TNodePointer;
begin
New(NewNode);
NewNode^.Item := Item;
NewNode^.Next := Top;
Top := NewNode;
end;
procedure Pop(var Top : TNodePointer; var Item : integer);
{I.S : Harga Top dan Item sudah didefinisikan}
{F.S : Menghapus item dari list}
var
DeletedNode: TNodePointer;
begin
DeletedNode := Top;
Item := DeletedNode^.Item;
if (not OneList(Top)) then
begin
Top := Top^.Next;
end
else
begin
Top := nil;
end;
Dispose(DeletedNode);
end;
procedure DestroyList(var Top : TNodePointer);
{I.S : Harga Top sudah didefinisikan}
{F.S : Melakukan penghancuran pada list}
var
DeletedNode: TNodePointer;
begin
DeletedNode := Top;
while (DeletedNode <> nil) do
begin
Top := Top^.Next;
Dispose(DeletedNode);
DeletedNode := Top;
end;
end;
procedure ActionPush(var Top : TNodePointer);
{I.S : Harga Top sudah didefinisikan}
{F.S : Melakukan aksi push}
var
Item: integer;
begin
clrscr;
PushMenu;
gotoxy(35, 4);
readln(Item);
Push(Top, Item);
ShowList(Top);
readln;
end;
procedure ActionPop(var Top : TNodePointer);
{I.S : Harga Top sudah didefinisikan}
{F.S : Melakukan aksi pop}
var
Item: integer;
begin
clrscr;
if (not Empty(Top)) then
begin
Pop(Top, Item);
PopHistory(Item);
ShowList(Top);
end
else
begin
writeln('Empty List');
writeln('Please Add New Note'); //ditambahkan
end;
readln;
end;
procedure ActionFor(Option : integer; var Top : TNodePointer);
{I.S : Harga Option dan Top sudah didefinisikan}
{F.S : Melakukan aksi pada menu utama}
begin
case Option of
1 : ActionPush(Top);
2 : ActionPop(Top);
0 : DestroyList(Top);
end;
end;
begin
Initialize(Top);
repeat
clrscr;
MainMenu;
ShowList(Top);
gotoxy(38, 4);
readln(Option);
ActionFor(Option, Top);
until Option = 0;
end.