因为 Squeak 是一个开源环境,我们可以看到如下数据结构的实现OrderedCollection>>addFirst
:
addFirst: newObject
"Add newObject to the beginning of the receiver. Answer newObject."
firstIndex = 1 ifTrue: [self makeRoomAtFirst].
firstIndex := firstIndex - 1.
array at: firstIndex put: newObject.
^ newObject
和OrderedCollection>>removeFirst
:
removeFirst: n
"Remove first n object into an array"
| list |
list := Array new: n.
1 to: n do: [:i |
list at: i put: self removeFirst].
^ list
然后我可以操作堆栈数据结构,对吗?
我被告知 Smalltalk 没有指针结构;尽管像 Java 这样的语言也没有指针结构,而不是脚本语言,但它应该实现一些基本的数据结构,如树、图(由Scripting: Higher Level Programming for the 21st Century引用),然后提出进一步的问题:
Smalltalk 是如何实现树形数据结构的?