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 |
|
241
|
24 |
$Header: /cvs/stx/stx/libbasic/Attic/LinkList.st,v 1.11 1995-02-08 03:11:01 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 |
"
|
241
|
45 |
$Header: /cvs/stx/stx/libbasic/Attic/LinkList.st,v 1.11 1995-02-08 03:11:01 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
this class implements an anchor to a list of Links.
|
|
52 |
The data itself is held in the Link elements. See Link, ValueLink
|
|
53 |
and (possibly other) subclasses.
|
|
54 |
"
|
|
55 |
! !
|
1
|
56 |
|
|
57 |
!LinkedList class methodsFor:'instance creation'!
|
|
58 |
|
|
59 |
new
|
|
60 |
"create and return a new LinkedList"
|
|
61 |
|
|
62 |
^ super new initialize
|
|
63 |
! !
|
|
64 |
|
|
65 |
!LinkedList methodsFor:'ininialization'!
|
|
66 |
|
|
67 |
initialize
|
|
68 |
numberOfNodes := 0
|
|
69 |
! !
|
|
70 |
|
|
71 |
!LinkedList methodsFor:'copying'!
|
|
72 |
|
159
|
73 |
XXXdeepCopy
|
13
|
74 |
"a kludge, to be removed when deepCopy handles recursive structures"
|
|
75 |
|
1
|
76 |
|newList|
|
159
|
77 |
|
1
|
78 |
newList := self shallowCopy.
|
159
|
79 |
newList setFirstNode:(firstLink deepCopy) lastNode:(firstLink last).
|
1
|
80 |
^ newList
|
159
|
81 |
|
|
82 |
"
|
|
83 |
|l|
|
|
84 |
l := LinkedList new.
|
|
85 |
l add:(ValueLink new value:1).
|
|
86 |
l add:(ValueLink new value:2).
|
|
87 |
l add:(ValueLink new value:3).
|
|
88 |
l inspect.
|
|
89 |
l deepCopy inspect
|
|
90 |
"
|
|
91 |
! !
|
|
92 |
|
|
93 |
!LinkedList methodsFor:'private accessing'!
|
|
94 |
|
|
95 |
XXsetFirstNode:first lastNode:last
|
|
96 |
"set the first node to be the argument, aNode"
|
|
97 |
|
|
98 |
firstLink := first.
|
|
99 |
lastLink := last
|
1
|
100 |
! !
|
|
101 |
|
|
102 |
!LinkedList methodsFor:'accessing'!
|
|
103 |
|
|
104 |
first
|
|
105 |
"return the first node in the list"
|
|
106 |
|
159
|
107 |
firstLink isNil ifTrue:[self emptyCollectionError].
|
1
|
108 |
^ firstLink
|
|
109 |
!
|
|
110 |
|
|
111 |
last
|
|
112 |
"return last node in the list"
|
|
113 |
|
159
|
114 |
lastLink isNil ifTrue:[self emptyCollectionError].
|
1
|
115 |
^ lastLink
|
159
|
116 |
!
|
|
117 |
|
|
118 |
at:index
|
|
119 |
"return the n'th element - use of this method should be avoided,
|
|
120 |
since it is slow to walk through the list - think about using
|
|
121 |
another collection if you need index access.
|
241
|
122 |
Notice, that many methods in SeqColl are based on at:-access,
|
|
123 |
so other inherited methods may be very slow (showing square runtime)."
|
159
|
124 |
|
|
125 |
|theLink
|
|
126 |
runIndex "{Class: SmallInteger}"|
|
|
127 |
|
|
128 |
theLink := firstLink.
|
|
129 |
runIndex := 1.
|
|
130 |
[theLink isNil or:[runIndex == index]] whileFalse:[
|
|
131 |
theLink := theLink nextLink.
|
|
132 |
runIndex := runIndex + 1.
|
|
133 |
].
|
|
134 |
theLink isNil ifTrue:[
|
|
135 |
^ self subscriptBoundsError:index
|
|
136 |
].
|
|
137 |
^ theLink
|
93
|
138 |
! !
|
|
139 |
|
|
140 |
!LinkedList methodsFor:'queries'!
|
|
141 |
|
1
|
142 |
size
|
|
143 |
"return the size of the LinkedList i.e. the number of nodes"
|
|
144 |
|
|
145 |
^ numberOfNodes
|
93
|
146 |
!
|
|
147 |
|
|
148 |
isEmpty
|
|
149 |
"return true, if the collection is empty"
|
|
150 |
|
|
151 |
^ firstLink isNil
|
1
|
152 |
! !
|
|
153 |
|
|
154 |
!LinkedList methodsFor:'testing'!
|
|
155 |
|
|
156 |
includes:anObject
|
|
157 |
"return true, if some nodes contents is anObject"
|
|
158 |
|
|
159 |
|theNode|
|
|
160 |
|
|
161 |
theNode := firstLink.
|
|
162 |
[theNode notNil] whileTrue:[
|
159
|
163 |
(anObject = theNode) ifTrue:[^ true].
|
|
164 |
theNode := theNode nextLink
|
1
|
165 |
].
|
|
166 |
^ false
|
|
167 |
! !
|
|
168 |
|
|
169 |
!LinkedList methodsFor:'adding/removing elements'!
|
|
170 |
|
|
171 |
addFirst:aLink
|
|
172 |
"adds aLink to the beginning of the sequence. Returns aLink"
|
|
173 |
|
|
174 |
firstLink isNil ifTrue:[
|
159
|
175 |
firstLink := aLink.
|
|
176 |
lastLink := aLink
|
1
|
177 |
] ifFalse: [
|
159
|
178 |
aLink nextLink:firstLink.
|
|
179 |
firstLink := aLink
|
1
|
180 |
].
|
|
181 |
numberOfNodes := numberOfNodes + 1.
|
|
182 |
^ aLink
|
|
183 |
!
|
|
184 |
|
|
185 |
add:aLink
|
|
186 |
"adds aLink to the end of the sequence. Returns aLink"
|
|
187 |
|
|
188 |
aLink nextLink:nil.
|
|
189 |
lastLink isNil ifTrue:[
|
159
|
190 |
firstLink := aLink
|
1
|
191 |
] ifFalse: [
|
159
|
192 |
lastLink nextLink:aLink
|
1
|
193 |
].
|
|
194 |
lastLink := aLink.
|
|
195 |
numberOfNodes := numberOfNodes + 1.
|
|
196 |
^ aLink
|
|
197 |
!
|
|
198 |
|
|
199 |
add:linkToAdd after:aLink
|
|
200 |
"adds linkToAdd after another link, aLink. If aLink is nil,
|
|
201 |
linkToAdd is inserted at the beginning. Returns linkToAdd."
|
|
202 |
|
|
203 |
|this|
|
|
204 |
|
159
|
205 |
aLink isNil ifTrue:[^ self addFirst:linkToAdd ].
|
1
|
206 |
|
|
207 |
this := firstLink.
|
|
208 |
[this notNil and:[this ~~ aLink]] whileTrue:[
|
159
|
209 |
this := this nextLink
|
1
|
210 |
].
|
159
|
211 |
this isNil ifTrue:[^ self add:linkToAdd ].
|
1
|
212 |
linkToAdd nextLink:(this nextLink).
|
|
213 |
this nextLink:linkToAdd.
|
|
214 |
^ linkToAdd
|
|
215 |
!
|
|
216 |
|
|
217 |
removeFirst
|
|
218 |
"remove and return the first node from the sequence"
|
|
219 |
|
|
220 |
|link|
|
|
221 |
|
|
222 |
firstLink isNil ifTrue:[
|
159
|
223 |
self errorIsEmpty
|
1
|
224 |
] ifFalse:[
|
159
|
225 |
link := firstLink.
|
|
226 |
(firstLink == lastLink) ifTrue:[
|
|
227 |
firstLink := nil.
|
|
228 |
lastLink := nil
|
|
229 |
] ifFalse:[
|
|
230 |
firstLink := firstLink nextLink
|
|
231 |
].
|
|
232 |
link nextLink:nil.
|
|
233 |
numberOfNodes := numberOfNodes - 1
|
1
|
234 |
].
|
|
235 |
^ link
|
|
236 |
!
|
|
237 |
|
|
238 |
remove:aLink ifAbsent:exceptionBlock
|
|
239 |
"remove the argument, aLink from the sequence; if absent,
|
175
|
240 |
evaluate the exceptionBlock"
|
1
|
241 |
|
|
242 |
|prevNode nextNode thisNode|
|
|
243 |
|
|
244 |
thisNode := firstLink.
|
|
245 |
[thisNode notNil] whileTrue:[
|
159
|
246 |
nextNode := thisNode nextLink.
|
|
247 |
(thisNode == aLink) ifTrue:[
|
|
248 |
prevNode isNil ifTrue:[
|
|
249 |
firstLink := thisNode nextLink
|
|
250 |
] ifFalse:[
|
|
251 |
prevNode nextLink:(thisNode nextLink)
|
|
252 |
].
|
|
253 |
(lastLink == thisNode) ifTrue:[
|
|
254 |
thisNode nextLink isNil ifTrue:[
|
|
255 |
lastLink := prevNode
|
|
256 |
] ifFalse:[
|
|
257 |
lastLink := thisNode nextLink
|
|
258 |
]
|
|
259 |
].
|
|
260 |
numberOfNodes := numberOfNodes - 1.
|
|
261 |
thisNode nextLink:nil.
|
175
|
262 |
^ aLink
|
159
|
263 |
].
|
|
264 |
prevNode := thisNode.
|
|
265 |
thisNode := nextNode
|
1
|
266 |
].
|
|
267 |
^ exceptionBlock value
|
|
268 |
! !
|
|
269 |
|
|
270 |
!LinkedList methodsFor:'enumerating'!
|
|
271 |
|
|
272 |
do:aBlock
|
|
273 |
"evaluate the argument, aBlock with 1 arg for every element in the list"
|
|
274 |
|
|
275 |
|thisNode|
|
|
276 |
|
|
277 |
thisNode := firstLink.
|
|
278 |
[thisNode notNil] whileTrue:[
|
159
|
279 |
aBlock value:thisNode.
|
|
280 |
thisNode := thisNode nextLink
|
1
|
281 |
]
|
|
282 |
!
|
|
283 |
|
|
284 |
reverseDo:aBlock fromNode:aNode
|
159
|
285 |
"helper for reverseDo: - this implementation is very expensive."
|
1
|
286 |
|
|
287 |
aNode notNil ifTrue:[
|
159
|
288 |
aNode nextLink notNil ifTrue:[
|
|
289 |
self reverseDo:aBlock fromNode:(aNode nextLink)
|
|
290 |
].
|
|
291 |
aBlock value:aNode
|
1
|
292 |
]
|
|
293 |
!
|
|
294 |
|
|
295 |
reverseDo:aBlock
|
|
296 |
"evaluate the argument, aBlock with 1 arg for every element in the list
|
|
297 |
in the reverse order"
|
|
298 |
|
|
299 |
self reverseDo:aBlock fromNode:firstLink
|
|
300 |
! !
|