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