type pt item item record data string 20 next pt end var stack pt integ

 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
type
pt = ^item;
item = record
data : string[20];
next : pt;
end;
var
stack : pt;
i : integer;
s : string;
procedure stack_push(var stack : pt; data : string[20]);
var
x : pt;
begin
New(x);
x^.data := data;
x^.next := stack;
stack := x;
end;
function stack_pop(var stack : pt) : string[20];
var
tmp : string[20];
x : pt;
begin
tmp := stack^.data;
x := stack;
stack := stack^.next;
Dispose(x);
stack_pop := tmp;
end;
procedure stack_print(stack : pt);
var
t : pt;
begin
t:=stack;
repeat
write(t^.data, ' ');
t:=t^.next;
until t=nil;
end;
begin
stack:=nil;
for i:=1 to 5 do
begin
str(i, s);
stack_push(stack, s);
end;
stack_print(stack);
writeln;
writeln(stack_pop(stack));
writeln(stack_pop(stack));
stack_print(stack);
end.