-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lists_Generic.adb
193 lines (171 loc) · 4.43 KB
/
Lists_Generic.adb
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
WITH Unchecked_Deallocation;
PACKAGE BODY Lists_Generic IS
PROCEDURE Dispose IS
NEW Unchecked_Deallocation(Object => Node, Name => Position);
FUNCTION Allocate (X: ElementType; P: Position) RETURN Position IS
Result: Position;
BEGIN
Result := NEW Node'(Info => X, Link => P);
RETURN Result;
EXCEPTION
WHEN Storage_Error =>
RAISE OutOfSpace;
END Allocate;
PROCEDURE Deallocate (P: IN OUT Position) IS
BEGIN
Dispose (X => P);
END Deallocate;
PROCEDURE Initialize(L: IN OUT List) IS
Previous: Position;
Current : Position;
BEGIN
IF L.Head /= NULL THEN
Current := L.Head;
WHILE Current /= NULL LOOP
Previous := Current;
Current := Current.Link;
Deallocate(Previous);
END LOOP;
L := (Head => NULL, Tail => NULL);
END IF;
END Initialize;
PROCEDURE AddToFront(L: IN OUT List; X: ElementType) IS
BEGIN
L.Head := Allocate(X, L.Head);
IF L.Tail = NULL THEN
L.Tail := L.Head;
END IF;
END AddToFront;
PROCEDURE AddToRear (L: IN OUT List; X: ElementType) IS
P: Position;
BEGIN
P := Allocate(X, NULL);
IF L.Head = NULL THEN
L.Head := P;
ELSE
L.Tail.Link := P;
END IF;
L.Tail := P;
END AddToRear;
FUNCTION IsEmpty (L: List) RETURN Boolean IS
BEGIN
RETURN L.Head = NULL;
END IsEmpty;
FUNCTION IsFirst (L: List; P: Position) RETURN Boolean IS
BEGIN
RETURN (L.Head /= NULL) AND (P = L.Head);
END IsFirst;
FUNCTION IsLast (L: List; P: Position) RETURN Boolean IS
BEGIN
RETURN (L.Tail /= NULL) AND (P = L.Tail);
END IsLast;
FUNCTION IsPastEnd (L: List; P: Position) RETURN Boolean IS
BEGIN
RETURN P = NULL;
END IsPastEnd;
FUNCTION IsPastBegin (L: List; P: Position) RETURN Boolean IS
BEGIN
RETURN P = NULL;
END IsPastBegin;
FUNCTION First (L: List) RETURN Position IS
BEGIN
RETURN L.Head;
END First;
FUNCTION Last (L: List) RETURN Position IS
BEGIN
RETURN L.Tail;
END Last;
FUNCTION Retrieve (L: IN List; P: IN Position) RETURN ElementType IS
BEGIN
IF IsEmpty(L) THEN
RAISE EmptyList;
ELSIF IsPastBegin(L, P) THEN
RAISE PastBegin;
ELSIF IsPastEnd(L, P) THEN
RAISE PastEnd;
ELSE
RETURN P.Info;
END IF;
END Retrieve;
PROCEDURE GoAhead (L: List; P: IN OUT Position) IS
BEGIN
IF IsEmpty(L) THEN
RAISE EmptyList;
ELSIF IsPastEnd(L, P) THEN
RAISE PastEnd;
ELSE
P := P.Link;
END IF;
END GoAhead;
PROCEDURE GoBack (L: List; P: IN OUT Position) IS
Current: Position;
BEGIN
IF IsEmpty(L) THEN
RAISE EmptyList;
ELSIF IsPastBegin(L, P) THEN
RAISE PastBegin;
ELSIF IsFirst(L, P) THEN
P := NULL;
ELSE -- see whether P is in the list
Current := L.Head;
WHILE (Current /= NULL) AND THEN (Current.Link /= P) LOOP
Current := Current.Link;
END LOOP;
IF Current = NULL THEN -- P was not in the list
RAISE PastEnd;
ELSE
P := Current; -- return predecessor pointer
END IF;
END IF;
END GoBack;
PROCEDURE Delete (L: IN OUT List; P: Position) IS
Previous: Position;
Current : Position;
BEGIN
Current := P;
IF IsEmpty(L) THEN
RAISE EmptyList;
ELSIF IsPastBegin(L, Current) THEN
RAISE PastBegin;
ELSIF IsFirst(L, Current) THEN -- must adjust list header
L.Head := Current.Link;
IF L.Head = NULL THEN -- deleted the only node
L.Tail := NULL;
END IF;
ELSE -- "normal" situation
Previous := Current;
GoBack(L, Previous);
Previous.Link := Current.Link;
IF IsLast(L, Current) THEN -- deleted the last node
L.Tail := Previous;
END IF;
END IF;
Deallocate(Current);
END Delete;
PROCEDURE Insert (L: IN OUT List; X: ElementType; P: Position) IS
BEGIN
IF P = NULL THEN
AddToRear(L, X);
ELSE
P.Link := Allocate(X, P.Link);
END IF;
END Insert;
PROCEDURE Replace (L: IN OUT List; X: ElementType; P: Position) IS
BEGIN
IF P = NULL THEN
RAISE PastEnd;
ELSE
P.Info := X;
END IF;
END Replace;
PROCEDURE Copy (To: IN OUT List; From: IN List) IS
Current: Position;
BEGIN
Initialize(To);
Current := First(From);
WHILE NOT IsPastEnd(From, Current) LOOP
AddToRear(To, Retrieve(From, Current));
GoAhead(From, Current);
END LOOP;
END Copy;
END Lists_Generic;