1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
SequenceableCollection subclass:#LinkedList
|
159
|
14 |
instanceVariableNames:'firstLink lastLink numberOfNodes'
|
1
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Sequenceable'
|
|
18 |
!
|
|
19 |
|
|
20 |
LinkedList comment:'
|
88
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
93
|
23 |
|
384
|
24 |
$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.17 1995-08-11 03:01:35 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!LinkedList class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
384
|
45 |
$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.17 1995-08-11 03:01:35 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
this class implements an anchor to a list of Links.
|
360
|
52 |
The data itself is held in the link elements.
|
243
|
53 |
See (the abstract) Link, ValueLink and (possibly other) classes,
|
|
54 |
which can be used as elements of a linkedList.
|
|
55 |
|
360
|
56 |
LinkedList does not care for storage; all it does is handling
|
|
57 |
chained link elements, which must respond to #nextLink/#nextLink:.
|
|
58 |
(i.e. any object which can do this, can be used as elements of a linked
|
|
59 |
list).
|
|
60 |
|
243
|
61 |
Although LinkedList is a subclass of SequenceableCollection (and therefore
|
|
62 |
supports indexed access via at:), you should be careful in using it or
|
360
|
63 |
other methods based upon at:.
|
|
64 |
The reason is that #at: walks the linkedlist to find the indexed element
|
|
65 |
and is therefore slow.
|
|
66 |
This means that some linear-in-time algorithms inherited from
|
|
67 |
SequenceableCollection become square in runtime.
|
243
|
68 |
In general, if you need access via a numeric index, you better use Array,
|
|
69 |
OrderedCollection or similar.
|
360
|
70 |
|
|
71 |
For the above reasons, the system does not make heavily use of LinkedLists;
|
|
72 |
the only good application is where elements must be repeatedly be removed
|
|
73 |
at the front and added at the end.
|
|
74 |
(the schedulers process handling code does this to manage process lists.)
|
|
75 |
"
|
|
76 |
!
|
|
77 |
|
|
78 |
examples
|
|
79 |
"
|
|
80 |
|l|
|
|
81 |
|
|
82 |
l := LinkedList new.
|
|
83 |
l addLast:(ValueLink new value:'one').
|
|
84 |
l addLast:(ValueLink new value:'two').
|
|
85 |
l addLast:(ValueLink new value:'three').
|
|
86 |
l addLast:(ValueLink new value:'four').
|
|
87 |
l inspect
|
|
88 |
|
|
89 |
|
|
90 |
|l|
|
|
91 |
|
|
92 |
l := LinkedList new.
|
|
93 |
l addLast:(ValueLink new value:'one').
|
|
94 |
l addLast:(ValueLink new value:'two').
|
|
95 |
l addLast:(ValueLink new value:'three').
|
|
96 |
l addLast:(ValueLink new value:'four').
|
|
97 |
(l at:3) value inspect. 'slow operation for large lists'.
|
|
98 |
|
|
99 |
|
|
100 |
|l link|
|
|
101 |
|
|
102 |
l := LinkedList new.
|
|
103 |
l addLast:(ValueLink new value:'one').
|
|
104 |
l addLast:(ValueLink new value:'two').
|
|
105 |
l addLast:(ValueLink new value:'three').
|
|
106 |
l addLast:(ValueLink new value:'four').
|
|
107 |
link := l removeFirst.
|
|
108 |
l addLast:link.
|
|
109 |
l inspect.
|
88
|
110 |
"
|
|
111 |
! !
|
1
|
112 |
|
|
113 |
!LinkedList class methodsFor:'instance creation'!
|
|
114 |
|
|
115 |
new
|
|
116 |
"create and return a new LinkedList"
|
|
117 |
|
|
118 |
^ super new initialize
|
|
119 |
! !
|
|
120 |
|
|
121 |
!LinkedList methodsFor:'ininialization'!
|
|
122 |
|
|
123 |
initialize
|
|
124 |
numberOfNodes := 0
|
|
125 |
! !
|
|
126 |
|
|
127 |
!LinkedList methodsFor:'accessing'!
|
|
128 |
|
|
129 |
first
|
|
130 |
"return the first node in the list"
|
|
131 |
|
159
|
132 |
firstLink isNil ifTrue:[self emptyCollectionError].
|
1
|
133 |
^ firstLink
|
|
134 |
!
|
|
135 |
|
|
136 |
last
|
|
137 |
"return last node in the list"
|
|
138 |
|
159
|
139 |
lastLink isNil ifTrue:[self emptyCollectionError].
|
1
|
140 |
^ lastLink
|
159
|
141 |
!
|
|
142 |
|
|
143 |
at:index
|
|
144 |
"return the n'th element - use of this method should be avoided,
|
|
145 |
since it is slow to walk through the list - think about using
|
|
146 |
another collection if you need index access.
|
241
|
147 |
Notice, that many methods in SeqColl are based on at:-access,
|
|
148 |
so other inherited methods may be very slow (showing square runtime)."
|
159
|
149 |
|
|
150 |
|theLink
|
|
151 |
runIndex "{Class: SmallInteger}"|
|
|
152 |
|
|
153 |
theLink := firstLink.
|
|
154 |
runIndex := 1.
|
243
|
155 |
[runIndex == index] whileFalse:[
|
|
156 |
theLink isNil ifTrue:[^ self subscriptBoundsError:index].
|
159
|
157 |
theLink := theLink nextLink.
|
|
158 |
runIndex := runIndex + 1.
|
|
159 |
].
|
|
160 |
^ theLink
|
93
|
161 |
! !
|
|
162 |
|
|
163 |
!LinkedList methodsFor:'queries'!
|
|
164 |
|
1
|
165 |
size
|
|
166 |
"return the size of the LinkedList i.e. the number of nodes"
|
|
167 |
|
|
168 |
^ numberOfNodes
|
93
|
169 |
!
|
|
170 |
|
|
171 |
isEmpty
|
|
172 |
"return true, if the collection is empty"
|
|
173 |
|
|
174 |
^ firstLink isNil
|
370
|
175 |
!
|
|
176 |
|
|
177 |
notEmpty
|
|
178 |
"return true, if the collection is not empty"
|
|
179 |
|
|
180 |
^ firstLink notNil
|
1
|
181 |
! !
|
|
182 |
|
|
183 |
!LinkedList methodsFor:'testing'!
|
|
184 |
|
243
|
185 |
indexOf:aLink startingAt:start
|
|
186 |
"search the collection for aLink, starting the search at index start;
|
|
187 |
if found, return the index otherwise return 0. Here, index is defined
|
|
188 |
as the link-nodes position in the list.
|
|
189 |
The comparison is done using = (i.e. equality test - not identity test)."
|
|
190 |
|
|
191 |
|theNode idx "{ Class: SmallInteger }"|
|
|
192 |
|
|
193 |
theNode := firstLink.
|
|
194 |
idx := 1.
|
|
195 |
[idx < start] whileTrue:[
|
|
196 |
theNode isNil ifTrue:[^ 0]. "reached the end"
|
|
197 |
theNode := theNode nextLink.
|
|
198 |
idx := idx + 1.
|
|
199 |
].
|
|
200 |
[theNode notNil] whileTrue:[
|
|
201 |
(aLink = theNode) ifTrue:[^ idx].
|
|
202 |
theNode := theNode nextLink.
|
|
203 |
idx := idx + 1.
|
|
204 |
]. "reached the end"
|
|
205 |
^ 0
|
|
206 |
|
|
207 |
"
|
|
208 |
|l|
|
|
209 |
|
|
210 |
l := LinkedList new.
|
|
211 |
l indexOf:'hello'
|
|
212 |
"
|
|
213 |
|
|
214 |
"
|
|
215 |
|l v|
|
|
216 |
|
|
217 |
l := LinkedList new.
|
|
218 |
l add:(ValueLink new value:'one').
|
|
219 |
l add:(ValueLink new value:'two').
|
|
220 |
l add:(v := ValueLink new value:'hello').
|
|
221 |
l indexOf:v
|
|
222 |
"
|
|
223 |
!
|
|
224 |
|
|
225 |
identityIndexOf:aLink startingAt:start
|
|
226 |
"search the collection for aLink, starting the search at index start;
|
|
227 |
if found, return the index otherwise return 0. Here, index is defined
|
|
228 |
as the link-nodes position in the list.
|
|
229 |
The comparison is done using ==
|
|
230 |
(i.e. equality test - not identity test)."
|
|
231 |
|
|
232 |
|theNode idx "{ Class: SmallInteger }"|
|
|
233 |
|
|
234 |
theNode := firstLink.
|
|
235 |
idx := 1.
|
|
236 |
[idx < start] whileTrue:[
|
|
237 |
theNode isNil ifTrue:[^ 0]. "reached the end"
|
|
238 |
theNode := theNode nextLink.
|
|
239 |
idx := idx + 1.
|
|
240 |
].
|
|
241 |
[theNode notNil] whileTrue:[
|
|
242 |
(aLink == theNode) ifTrue:[^ idx].
|
|
243 |
theNode := theNode nextLink.
|
|
244 |
idx := idx + 1.
|
|
245 |
]. "reached the end"
|
|
246 |
^ 0
|
|
247 |
|
|
248 |
"
|
|
249 |
|l|
|
|
250 |
|
|
251 |
l := LinkedList new.
|
|
252 |
l indexOf:'hello'
|
|
253 |
"
|
|
254 |
|
|
255 |
"
|
|
256 |
|l v|
|
|
257 |
|
|
258 |
l := LinkedList new.
|
|
259 |
l add:(ValueLink new value:'one').
|
|
260 |
l add:(ValueLink new value:'two').
|
|
261 |
l add:(v := ValueLink new value:'hello').
|
|
262 |
l identityIndexOf:v
|
|
263 |
"
|
1
|
264 |
! !
|
|
265 |
|
367
|
266 |
!LinkedList methodsFor:'adding & removing'!
|
1
|
267 |
|
|
268 |
addFirst:aLink
|
|
269 |
"adds aLink to the beginning of the sequence. Returns aLink"
|
|
270 |
|
|
271 |
firstLink isNil ifTrue:[
|
159
|
272 |
firstLink := aLink.
|
|
273 |
lastLink := aLink
|
1
|
274 |
] ifFalse: [
|
159
|
275 |
aLink nextLink:firstLink.
|
|
276 |
firstLink := aLink
|
1
|
277 |
].
|
|
278 |
numberOfNodes := numberOfNodes + 1.
|
|
279 |
^ aLink
|
|
280 |
!
|
|
281 |
|
|
282 |
add:aLink
|
|
283 |
"adds aLink to the end of the sequence. Returns aLink"
|
|
284 |
|
|
285 |
aLink nextLink:nil.
|
|
286 |
lastLink isNil ifTrue:[
|
159
|
287 |
firstLink := aLink
|
1
|
288 |
] ifFalse: [
|
159
|
289 |
lastLink nextLink:aLink
|
1
|
290 |
].
|
|
291 |
lastLink := aLink.
|
|
292 |
numberOfNodes := numberOfNodes + 1.
|
|
293 |
^ aLink
|
|
294 |
!
|
|
295 |
|
|
296 |
add:linkToAdd after:aLink
|
|
297 |
"adds linkToAdd after another link, aLink. If aLink is nil,
|
|
298 |
linkToAdd is inserted at the beginning. Returns linkToAdd."
|
|
299 |
|
|
300 |
|this|
|
|
301 |
|
159
|
302 |
aLink isNil ifTrue:[^ self addFirst:linkToAdd ].
|
1
|
303 |
|
|
304 |
this := firstLink.
|
|
305 |
[this notNil and:[this ~~ aLink]] whileTrue:[
|
159
|
306 |
this := this nextLink
|
1
|
307 |
].
|
159
|
308 |
this isNil ifTrue:[^ self add:linkToAdd ].
|
1
|
309 |
linkToAdd nextLink:(this nextLink).
|
|
310 |
this nextLink:linkToAdd.
|
|
311 |
^ linkToAdd
|
|
312 |
!
|
|
313 |
|
|
314 |
removeFirst
|
|
315 |
"remove and return the first node from the sequence"
|
|
316 |
|
|
317 |
|link|
|
|
318 |
|
|
319 |
firstLink isNil ifTrue:[
|
159
|
320 |
self errorIsEmpty
|
1
|
321 |
] ifFalse:[
|
159
|
322 |
link := firstLink.
|
|
323 |
(firstLink == lastLink) ifTrue:[
|
|
324 |
firstLink := nil.
|
|
325 |
lastLink := nil
|
|
326 |
] ifFalse:[
|
|
327 |
firstLink := firstLink nextLink
|
|
328 |
].
|
|
329 |
link nextLink:nil.
|
|
330 |
numberOfNodes := numberOfNodes - 1
|
1
|
331 |
].
|
|
332 |
^ link
|
|
333 |
!
|
|
334 |
|
|
335 |
remove:aLink ifAbsent:exceptionBlock
|
|
336 |
"remove the argument, aLink from the sequence; if absent,
|
175
|
337 |
evaluate the exceptionBlock"
|
1
|
338 |
|
|
339 |
|prevNode nextNode thisNode|
|
|
340 |
|
|
341 |
thisNode := firstLink.
|
|
342 |
[thisNode notNil] whileTrue:[
|
159
|
343 |
nextNode := thisNode nextLink.
|
|
344 |
(thisNode == aLink) ifTrue:[
|
|
345 |
prevNode isNil ifTrue:[
|
|
346 |
firstLink := thisNode nextLink
|
|
347 |
] ifFalse:[
|
|
348 |
prevNode nextLink:(thisNode nextLink)
|
|
349 |
].
|
|
350 |
(lastLink == thisNode) ifTrue:[
|
|
351 |
thisNode nextLink isNil ifTrue:[
|
|
352 |
lastLink := prevNode
|
|
353 |
] ifFalse:[
|
|
354 |
lastLink := thisNode nextLink
|
|
355 |
]
|
|
356 |
].
|
|
357 |
numberOfNodes := numberOfNodes - 1.
|
|
358 |
thisNode nextLink:nil.
|
175
|
359 |
^ aLink
|
159
|
360 |
].
|
|
361 |
prevNode := thisNode.
|
|
362 |
thisNode := nextNode
|
1
|
363 |
].
|
|
364 |
^ exceptionBlock value
|
|
365 |
! !
|
|
366 |
|
|
367 |
!LinkedList methodsFor:'enumerating'!
|
|
368 |
|
|
369 |
do:aBlock
|
|
370 |
"evaluate the argument, aBlock with 1 arg for every element in the list"
|
|
371 |
|
|
372 |
|thisNode|
|
|
373 |
|
|
374 |
thisNode := firstLink.
|
|
375 |
[thisNode notNil] whileTrue:[
|
159
|
376 |
aBlock value:thisNode.
|
|
377 |
thisNode := thisNode nextLink
|
1
|
378 |
]
|
|
379 |
!
|
|
380 |
|
|
381 |
reverseDo:aBlock fromNode:aNode
|
159
|
382 |
"helper for reverseDo: - this implementation is very expensive."
|
1
|
383 |
|
|
384 |
aNode notNil ifTrue:[
|
159
|
385 |
aNode nextLink notNil ifTrue:[
|
|
386 |
self reverseDo:aBlock fromNode:(aNode nextLink)
|
|
387 |
].
|
|
388 |
aBlock value:aNode
|
1
|
389 |
]
|
|
390 |
!
|
|
391 |
|
|
392 |
reverseDo:aBlock
|
|
393 |
"evaluate the argument, aBlock with 1 arg for every element in the list
|
|
394 |
in the reverse order"
|
|
395 |
|
|
396 |
self reverseDo:aBlock fromNode:firstLink
|
|
397 |
! !
|