1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'firstLink lastLink nodeClass numberOfNodes'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Sequenceable'
|
|
18 |
!
|
|
19 |
|
|
20 |
LinkedList comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
this class implements an anchor to a list of Links.
|
|
26 |
The data itself is held in the Link elements (see Link and subclasses).
|
|
27 |
|
10
|
28 |
$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.5 1993-11-08 02:30:50 claus Exp $
|
1
|
29 |
'!
|
|
30 |
|
|
31 |
!LinkedList class methodsFor:'instance creation'!
|
|
32 |
|
|
33 |
new
|
|
34 |
"create and return a new LinkedList"
|
|
35 |
|
|
36 |
^ super new initialize
|
|
37 |
! !
|
|
38 |
|
|
39 |
!LinkedList methodsFor:'ininialization'!
|
|
40 |
|
|
41 |
initialize
|
|
42 |
numberOfNodes := 0
|
|
43 |
! !
|
|
44 |
|
|
45 |
!LinkedList methodsFor:'copying'!
|
|
46 |
|
|
47 |
deepCopy
|
|
48 |
|newList|
|
|
49 |
newList := self shallowCopy.
|
|
50 |
newList setFirstNode:(firstLink deepCopy).
|
|
51 |
newList setLastNode:(firstLink last).
|
|
52 |
^ newList
|
|
53 |
! !
|
|
54 |
|
|
55 |
!LinkedList methodsFor:'accessing'!
|
|
56 |
|
|
57 |
setFirstNode:aNode
|
|
58 |
"set the first node to be the argument, aNode"
|
|
59 |
|
|
60 |
firstLink := aNode
|
|
61 |
!
|
|
62 |
|
|
63 |
setLastNode:aNode
|
|
64 |
"set the last node to be the argument, aNode"
|
|
65 |
|
|
66 |
lastLink := aNode
|
|
67 |
!
|
|
68 |
|
|
69 |
first
|
|
70 |
"return the first node in the list"
|
|
71 |
|
|
72 |
^ firstLink
|
|
73 |
!
|
|
74 |
|
|
75 |
last
|
|
76 |
"return last node in the list"
|
|
77 |
|
|
78 |
^ lastLink
|
|
79 |
!
|
|
80 |
|
|
81 |
size
|
|
82 |
"return the size of the LinkedList i.e. the number of nodes"
|
|
83 |
|
|
84 |
^ numberOfNodes
|
|
85 |
! !
|
|
86 |
|
|
87 |
!LinkedList methodsFor:'testing'!
|
|
88 |
|
|
89 |
includes:anObject
|
|
90 |
"return true, if some nodes contents is anObject"
|
|
91 |
|
|
92 |
|theNode|
|
|
93 |
|
|
94 |
theNode := firstLink.
|
|
95 |
[theNode notNil] whileTrue:[
|
|
96 |
(anObject = theNode) ifTrue:[^ true].
|
|
97 |
theNode := theNode nextLink
|
|
98 |
].
|
|
99 |
^ false
|
|
100 |
! !
|
|
101 |
|
|
102 |
!LinkedList methodsFor:'adding/removing elements'!
|
|
103 |
|
|
104 |
addFirst:aLink
|
|
105 |
"adds aLink to the beginning of the sequence. Returns aLink"
|
|
106 |
|
|
107 |
firstLink isNil ifTrue:[
|
|
108 |
firstLink := aLink.
|
|
109 |
lastLink := aLink
|
|
110 |
] ifFalse: [
|
|
111 |
aLink nextLink:firstLink.
|
|
112 |
firstLink := aLink
|
|
113 |
].
|
|
114 |
numberOfNodes := numberOfNodes + 1.
|
|
115 |
^ aLink
|
|
116 |
!
|
|
117 |
|
|
118 |
add:aLink
|
|
119 |
"adds aLink to the end of the sequence. Returns aLink"
|
|
120 |
|
|
121 |
aLink nextLink:nil.
|
|
122 |
lastLink isNil ifTrue:[
|
|
123 |
firstLink := aLink
|
|
124 |
] ifFalse: [
|
|
125 |
lastLink nextLink:aLink
|
|
126 |
].
|
|
127 |
lastLink := aLink.
|
|
128 |
numberOfNodes := numberOfNodes + 1.
|
|
129 |
^ aLink
|
|
130 |
!
|
|
131 |
|
|
132 |
add:linkToAdd after:aLink
|
|
133 |
"adds linkToAdd after another link, aLink. If aLink is nil,
|
|
134 |
linkToAdd is inserted at the beginning. Returns linkToAdd."
|
|
135 |
|
|
136 |
|this|
|
|
137 |
|
|
138 |
aLink isNil ifTrue:[ ^ self addFirst:linkToAdd ].
|
|
139 |
|
|
140 |
this := firstLink.
|
|
141 |
[this notNil and:[this ~~ aLink]] whileTrue:[
|
|
142 |
this := this nextLink
|
|
143 |
].
|
|
144 |
this isNil ifTrue:[ ^ self addLast:linkToAdd ].
|
|
145 |
linkToAdd nextLink:(this nextLink).
|
|
146 |
this nextLink:linkToAdd.
|
|
147 |
^ linkToAdd
|
|
148 |
!
|
|
149 |
|
|
150 |
removeFirst
|
|
151 |
"remove and return the first node from the sequence"
|
|
152 |
|
|
153 |
|link|
|
|
154 |
|
|
155 |
firstLink isNil ifTrue:[
|
|
156 |
self errorIsEmpty
|
|
157 |
] ifFalse:[
|
|
158 |
link := firstLink.
|
|
159 |
(firstLink == lastLink) ifTrue:[
|
|
160 |
firstLink := nil.
|
|
161 |
lastLink := nil
|
|
162 |
] ifFalse:[
|
|
163 |
firstLink := firstLink nextLink
|
|
164 |
].
|
10
|
165 |
link nextLink:nil.
|
1
|
166 |
numberOfNodes := numberOfNodes - 1
|
|
167 |
].
|
|
168 |
^ link
|
|
169 |
!
|
|
170 |
|
|
171 |
remove:aLink ifAbsent:exceptionBlock
|
|
172 |
"remove the argument, aLink from the sequence; if absent,
|
|
173 |
evaluate the excpetionBlock"
|
|
174 |
|
|
175 |
|prevNode nextNode thisNode|
|
|
176 |
|
|
177 |
thisNode := firstLink.
|
|
178 |
[thisNode notNil] whileTrue:[
|
|
179 |
nextNode := thisNode nextLink.
|
|
180 |
(thisNode == aLink) ifTrue:[
|
|
181 |
prevNode isNil ifTrue:[
|
|
182 |
firstLink := thisNode nextLink
|
|
183 |
] ifFalse:[
|
|
184 |
prevNode nextLink:(thisNode nextLink)
|
|
185 |
].
|
|
186 |
(lastLink == thisNode) ifTrue:[
|
|
187 |
thisNode nextLink isNil ifTrue:[
|
|
188 |
lastLink := prevNode
|
|
189 |
] ifFalse:[
|
|
190 |
lastLink := thisNode nextLink
|
|
191 |
]
|
|
192 |
].
|
|
193 |
numberOfNodes := numberOfNodes - 1.
|
10
|
194 |
thisNode nextLink:nil.
|
1
|
195 |
^ self
|
|
196 |
].
|
|
197 |
prevNode := thisNode.
|
|
198 |
thisNode := nextNode
|
|
199 |
].
|
|
200 |
^ exceptionBlock value
|
|
201 |
! !
|
|
202 |
|
|
203 |
!LinkedList methodsFor:'enumerating'!
|
|
204 |
|
|
205 |
do:aBlock
|
|
206 |
"evaluate the argument, aBlock with 1 arg for every element in the list"
|
|
207 |
|
|
208 |
|thisNode|
|
|
209 |
|
|
210 |
thisNode := firstLink.
|
|
211 |
[thisNode notNil] whileTrue:[
|
|
212 |
aBlock value:thisNode.
|
|
213 |
thisNode := thisNode nextLink
|
|
214 |
]
|
|
215 |
!
|
|
216 |
|
|
217 |
reverseDo:aBlock fromNode:aNode
|
|
218 |
"helper for reverseDo:"
|
|
219 |
|
|
220 |
aNode notNil ifTrue:[
|
|
221 |
aNode nextLink notNil ifTrue:[
|
|
222 |
self reverseDo:aBlock fromNode:(aNode nextLink)
|
|
223 |
].
|
|
224 |
aBlock value:aNode
|
|
225 |
]
|
|
226 |
!
|
|
227 |
|
|
228 |
reverseDo:aBlock
|
|
229 |
"evaluate the argument, aBlock with 1 arg for every element in the list
|
|
230 |
in the reverse order"
|
|
231 |
|
|
232 |
self reverseDo:aBlock fromNode:firstLink
|
|
233 |
! !
|