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 |
|
243
|
24 |
$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.12 1995-02-11 14:08:37 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 |
"
|
243
|
45 |
$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.12 1995-02-11 14:08:37 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
this class implements an anchor to a list of Links.
|
243
|
52 |
The data itself is held in the Link elements.
|
|
53 |
See (the abstract) Link, ValueLink and (possibly other) classes,
|
|
54 |
which can be used as elements of a linkedList.
|
|
55 |
|
|
56 |
Although LinkedList is a subclass of SequenceableCollection (and therefore
|
|
57 |
supports indexed access via at:), you should be careful in using it or
|
|
58 |
other methods based upon at:. The reason is that at: walks the linkedlist,
|
|
59 |
and is therefore slow. Also, linear in time algorithms inherited from
|
|
60 |
SequenceableCollection become square in runtime due to this.
|
|
61 |
In general, if you need access via a numeric index, you better use Array,
|
|
62 |
OrderedCollection or similar.
|
88
|
63 |
"
|
|
64 |
! !
|
1
|
65 |
|
|
66 |
!LinkedList class methodsFor:'instance creation'!
|
|
67 |
|
|
68 |
new
|
|
69 |
"create and return a new LinkedList"
|
|
70 |
|
|
71 |
^ super new initialize
|
|
72 |
! !
|
|
73 |
|
|
74 |
!LinkedList methodsFor:'ininialization'!
|
|
75 |
|
|
76 |
initialize
|
|
77 |
numberOfNodes := 0
|
|
78 |
! !
|
|
79 |
|
|
80 |
!LinkedList methodsFor:'accessing'!
|
|
81 |
|
|
82 |
first
|
|
83 |
"return the first node in the list"
|
|
84 |
|
159
|
85 |
firstLink isNil ifTrue:[self emptyCollectionError].
|
1
|
86 |
^ firstLink
|
|
87 |
!
|
|
88 |
|
|
89 |
last
|
|
90 |
"return last node in the list"
|
|
91 |
|
159
|
92 |
lastLink isNil ifTrue:[self emptyCollectionError].
|
1
|
93 |
^ lastLink
|
159
|
94 |
!
|
|
95 |
|
|
96 |
at:index
|
|
97 |
"return the n'th element - use of this method should be avoided,
|
|
98 |
since it is slow to walk through the list - think about using
|
|
99 |
another collection if you need index access.
|
241
|
100 |
Notice, that many methods in SeqColl are based on at:-access,
|
|
101 |
so other inherited methods may be very slow (showing square runtime)."
|
159
|
102 |
|
|
103 |
|theLink
|
|
104 |
runIndex "{Class: SmallInteger}"|
|
|
105 |
|
|
106 |
theLink := firstLink.
|
|
107 |
runIndex := 1.
|
243
|
108 |
[runIndex == index] whileFalse:[
|
|
109 |
theLink isNil ifTrue:[^ self subscriptBoundsError:index].
|
159
|
110 |
theLink := theLink nextLink.
|
|
111 |
runIndex := runIndex + 1.
|
|
112 |
].
|
|
113 |
^ theLink
|
93
|
114 |
! !
|
|
115 |
|
|
116 |
!LinkedList methodsFor:'queries'!
|
|
117 |
|
1
|
118 |
size
|
|
119 |
"return the size of the LinkedList i.e. the number of nodes"
|
|
120 |
|
|
121 |
^ numberOfNodes
|
93
|
122 |
!
|
|
123 |
|
|
124 |
isEmpty
|
|
125 |
"return true, if the collection is empty"
|
|
126 |
|
|
127 |
^ firstLink isNil
|
1
|
128 |
! !
|
|
129 |
|
|
130 |
!LinkedList methodsFor:'testing'!
|
|
131 |
|
|
132 |
includes:anObject
|
|
133 |
"return true, if some nodes contents is anObject"
|
|
134 |
|
|
135 |
|theNode|
|
|
136 |
|
|
137 |
theNode := firstLink.
|
|
138 |
[theNode notNil] whileTrue:[
|
159
|
139 |
(anObject = theNode) ifTrue:[^ true].
|
|
140 |
theNode := theNode nextLink
|
1
|
141 |
].
|
|
142 |
^ false
|
243
|
143 |
!
|
|
144 |
|
|
145 |
indexOf:aLink startingAt:start
|
|
146 |
"search the collection for aLink, starting the search at index start;
|
|
147 |
if found, return the index otherwise return 0. Here, index is defined
|
|
148 |
as the link-nodes position in the list.
|
|
149 |
The comparison is done using = (i.e. equality test - not identity test)."
|
|
150 |
|
|
151 |
|theNode idx "{ Class: SmallInteger }"|
|
|
152 |
|
|
153 |
theNode := firstLink.
|
|
154 |
idx := 1.
|
|
155 |
[idx < start] whileTrue:[
|
|
156 |
theNode isNil ifTrue:[^ 0]. "reached the end"
|
|
157 |
theNode := theNode nextLink.
|
|
158 |
idx := idx + 1.
|
|
159 |
].
|
|
160 |
[theNode notNil] whileTrue:[
|
|
161 |
(aLink = theNode) ifTrue:[^ idx].
|
|
162 |
theNode := theNode nextLink.
|
|
163 |
idx := idx + 1.
|
|
164 |
]. "reached the end"
|
|
165 |
^ 0
|
|
166 |
|
|
167 |
"
|
|
168 |
|l|
|
|
169 |
|
|
170 |
l := LinkedList new.
|
|
171 |
l indexOf:'hello'
|
|
172 |
"
|
|
173 |
|
|
174 |
"
|
|
175 |
|l v|
|
|
176 |
|
|
177 |
l := LinkedList new.
|
|
178 |
l add:(ValueLink new value:'one').
|
|
179 |
l add:(ValueLink new value:'two').
|
|
180 |
l add:(v := ValueLink new value:'hello').
|
|
181 |
l indexOf:v
|
|
182 |
"
|
|
183 |
!
|
|
184 |
|
|
185 |
identityIndexOf: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 ==
|
|
190 |
(i.e. equality test - not identity test)."
|
|
191 |
|
|
192 |
|theNode idx "{ Class: SmallInteger }"|
|
|
193 |
|
|
194 |
theNode := firstLink.
|
|
195 |
idx := 1.
|
|
196 |
[idx < start] whileTrue:[
|
|
197 |
theNode isNil ifTrue:[^ 0]. "reached the end"
|
|
198 |
theNode := theNode nextLink.
|
|
199 |
idx := idx + 1.
|
|
200 |
].
|
|
201 |
[theNode notNil] whileTrue:[
|
|
202 |
(aLink == theNode) ifTrue:[^ idx].
|
|
203 |
theNode := theNode nextLink.
|
|
204 |
idx := idx + 1.
|
|
205 |
]. "reached the end"
|
|
206 |
^ 0
|
|
207 |
|
|
208 |
"
|
|
209 |
|l|
|
|
210 |
|
|
211 |
l := LinkedList new.
|
|
212 |
l indexOf:'hello'
|
|
213 |
"
|
|
214 |
|
|
215 |
"
|
|
216 |
|l v|
|
|
217 |
|
|
218 |
l := LinkedList new.
|
|
219 |
l add:(ValueLink new value:'one').
|
|
220 |
l add:(ValueLink new value:'two').
|
|
221 |
l add:(v := ValueLink new value:'hello').
|
|
222 |
l identityIndexOf:v
|
|
223 |
"
|
1
|
224 |
! !
|
|
225 |
|
|
226 |
!LinkedList methodsFor:'adding/removing elements'!
|
|
227 |
|
|
228 |
addFirst:aLink
|
|
229 |
"adds aLink to the beginning of the sequence. Returns aLink"
|
|
230 |
|
|
231 |
firstLink isNil ifTrue:[
|
159
|
232 |
firstLink := aLink.
|
|
233 |
lastLink := aLink
|
1
|
234 |
] ifFalse: [
|
159
|
235 |
aLink nextLink:firstLink.
|
|
236 |
firstLink := aLink
|
1
|
237 |
].
|
|
238 |
numberOfNodes := numberOfNodes + 1.
|
|
239 |
^ aLink
|
|
240 |
!
|
|
241 |
|
|
242 |
add:aLink
|
|
243 |
"adds aLink to the end of the sequence. Returns aLink"
|
|
244 |
|
|
245 |
aLink nextLink:nil.
|
|
246 |
lastLink isNil ifTrue:[
|
159
|
247 |
firstLink := aLink
|
1
|
248 |
] ifFalse: [
|
159
|
249 |
lastLink nextLink:aLink
|
1
|
250 |
].
|
|
251 |
lastLink := aLink.
|
|
252 |
numberOfNodes := numberOfNodes + 1.
|
|
253 |
^ aLink
|
|
254 |
!
|
|
255 |
|
|
256 |
add:linkToAdd after:aLink
|
|
257 |
"adds linkToAdd after another link, aLink. If aLink is nil,
|
|
258 |
linkToAdd is inserted at the beginning. Returns linkToAdd."
|
|
259 |
|
|
260 |
|this|
|
|
261 |
|
159
|
262 |
aLink isNil ifTrue:[^ self addFirst:linkToAdd ].
|
1
|
263 |
|
|
264 |
this := firstLink.
|
|
265 |
[this notNil and:[this ~~ aLink]] whileTrue:[
|
159
|
266 |
this := this nextLink
|
1
|
267 |
].
|
159
|
268 |
this isNil ifTrue:[^ self add:linkToAdd ].
|
1
|
269 |
linkToAdd nextLink:(this nextLink).
|
|
270 |
this nextLink:linkToAdd.
|
|
271 |
^ linkToAdd
|
|
272 |
!
|
|
273 |
|
|
274 |
removeFirst
|
|
275 |
"remove and return the first node from the sequence"
|
|
276 |
|
|
277 |
|link|
|
|
278 |
|
|
279 |
firstLink isNil ifTrue:[
|
159
|
280 |
self errorIsEmpty
|
1
|
281 |
] ifFalse:[
|
159
|
282 |
link := firstLink.
|
|
283 |
(firstLink == lastLink) ifTrue:[
|
|
284 |
firstLink := nil.
|
|
285 |
lastLink := nil
|
|
286 |
] ifFalse:[
|
|
287 |
firstLink := firstLink nextLink
|
|
288 |
].
|
|
289 |
link nextLink:nil.
|
|
290 |
numberOfNodes := numberOfNodes - 1
|
1
|
291 |
].
|
|
292 |
^ link
|
|
293 |
!
|
|
294 |
|
|
295 |
remove:aLink ifAbsent:exceptionBlock
|
|
296 |
"remove the argument, aLink from the sequence; if absent,
|
175
|
297 |
evaluate the exceptionBlock"
|
1
|
298 |
|
|
299 |
|prevNode nextNode thisNode|
|
|
300 |
|
|
301 |
thisNode := firstLink.
|
|
302 |
[thisNode notNil] whileTrue:[
|
159
|
303 |
nextNode := thisNode nextLink.
|
|
304 |
(thisNode == aLink) ifTrue:[
|
|
305 |
prevNode isNil ifTrue:[
|
|
306 |
firstLink := thisNode nextLink
|
|
307 |
] ifFalse:[
|
|
308 |
prevNode nextLink:(thisNode nextLink)
|
|
309 |
].
|
|
310 |
(lastLink == thisNode) ifTrue:[
|
|
311 |
thisNode nextLink isNil ifTrue:[
|
|
312 |
lastLink := prevNode
|
|
313 |
] ifFalse:[
|
|
314 |
lastLink := thisNode nextLink
|
|
315 |
]
|
|
316 |
].
|
|
317 |
numberOfNodes := numberOfNodes - 1.
|
|
318 |
thisNode nextLink:nil.
|
175
|
319 |
^ aLink
|
159
|
320 |
].
|
|
321 |
prevNode := thisNode.
|
|
322 |
thisNode := nextNode
|
1
|
323 |
].
|
|
324 |
^ exceptionBlock value
|
|
325 |
! !
|
|
326 |
|
|
327 |
!LinkedList methodsFor:'enumerating'!
|
|
328 |
|
|
329 |
do:aBlock
|
|
330 |
"evaluate the argument, aBlock with 1 arg for every element in the list"
|
|
331 |
|
|
332 |
|thisNode|
|
|
333 |
|
|
334 |
thisNode := firstLink.
|
|
335 |
[thisNode notNil] whileTrue:[
|
159
|
336 |
aBlock value:thisNode.
|
|
337 |
thisNode := thisNode nextLink
|
1
|
338 |
]
|
|
339 |
!
|
|
340 |
|
|
341 |
reverseDo:aBlock fromNode:aNode
|
159
|
342 |
"helper for reverseDo: - this implementation is very expensive."
|
1
|
343 |
|
|
344 |
aNode notNil ifTrue:[
|
159
|
345 |
aNode nextLink notNil ifTrue:[
|
|
346 |
self reverseDo:aBlock fromNode:(aNode nextLink)
|
|
347 |
].
|
|
348 |
aBlock value:aNode
|
1
|
349 |
]
|
|
350 |
!
|
|
351 |
|
|
352 |
reverseDo:aBlock
|
|
353 |
"evaluate the argument, aBlock with 1 arg for every element in the list
|
|
354 |
in the reverse order"
|
|
355 |
|
|
356 |
self reverseDo:aBlock fromNode:firstLink
|
|
357 |
! !
|