ex5 by Bel

 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
program ex5;
uses crt;
type
tree=^rec1;
rec1=record
inf:string[10];
lf,rt:tree
end;
tlist=^rec2;
rec2=record
inf:string[10];
next:tlist
end;
var gltree:tree; head,p1:tlist; f1:text; q1:boolean;
procedure put_tree(var root:tree; inp:string);
var p,prev:tree; f:text; s:string;
begin
assign(f,inp);
reset(f);
if eof(f)
then root:=nil
else
begin
new(root);
readln(f,root^.inf);
root^.lf:=nil;
root^.rt:=nil;
end;
while not eof(f) do
begin
p:=root;
readln(f,s);
while p<>nil do
begin
prev:=p;
if s<=p^.inf
then p:=p^.lf
else p:=p^.rt
end;
new(p);
p^.lf:=nil;
p^.rt:=nil;
p^.inf:=s;
if p^.inf<prev^.inf
then prev^.lf:=p
else prev^.rt:=p
end;
close(f)
end;
procedure print_tree(p:tree);
begin
if p^.lf<>nil then print_tree(p^.lf);
writeln(p^.inf);
if p^.rt<>nil then print_tree(p^.rt)
end;
procedure make_sortlist(t:tree; var first,p: tlist; var q:boolean);
begin
if t^.lf<>nil then make_sortlist(t^.lf,first,p,q);
new(p^.next);
p:=p^.next;
p^.inf:=t^.inf;
p^.next:=nil;
if q=false
then
begin
first:=p;
q:=true
end;
if t^.rt<>nil then make_sortlist(t^.rt,first,p,q);
end;
begin
clrscr;
put_tree(gltree,'input5.inp');
print_tree(gltree);
p1:=nil;
q1:=false;
make_sortlist(gltree,head,p1,q1);
assign(f1,'output5.out');
rewrite(f1);
p1:=head;
while p1<>nil do
begin
writeln(f1,p1^.inf);
p1:=p1^.next
end;
close(f1)
end.