如果Squeak不是脚本语言,那么树形数据结构在哪里?
因为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这样的语言也没有指针结构,但不像脚本语言,它应该实现一些基本的数据结构,如树、图(由脚本编写:高级编程 对于21世纪),那么进一步的问题是:
Smalltalk如何实现树形数据结构?
Because Squeak is a open source environment, we can see the implementation of data structures like 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
and 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
Then I can manipulate a stack data structure, correct?
I am informed that Smalltalk has no pointer structure; though languages like Java also have no pointer structure, Not as a script language, it should implement some fundamental data structure like tree, diagram (referred by Scripting: Higher Level Programming
for the 21st Century), then rise the further question :
How does Smalltalk implement a tree data structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Smalltalk 到处都有指针,就像 Java 一样。您无法执行类似 C 的操作,例如递增指针,但
myVar := OrderedCollection new
意味着myVar
是一个指向空OrderedCollection
的指针>。是的,您可以使用 OrderedCollection 模拟堆栈,使用
addFirst:
推送元素,使用removeFirst:
和removeFirst
弹出元素。 (类似地,您可以通过使用addFirst:
推送元素并使用removeLast
删除它们来模拟队列。您可以像在任何语言中一样在 Smalltalk 中实现树。对于例如,我编写了一个非常基本的树实现来玩弄zippers。看看类
ZTree
,它实现了一个非常通用的树结构 - 一个节点可以有任意数量的孩子。Smalltalk has pointers everywhere, just like Java does. You can't do C-like things like increment pointers, but
myVar := OrderedCollection new
means thatmyVar
is a pointer pointing to an emptyOrderedCollection
.Yes, you can simulate a stack using OrderedCollection by using
addFirst:
to push elements andremoveFirst:
andremoveFirst
to pop elements. (Similarly, you can simulate a queue by pushing elements withaddFirst:
and removing them withremoveLast
.You can implement trees in Smalltalk the same way you do in any language. For instance, I wrote a very basic tree implementation for playing around with zippers. Look at class
ZTree
, which implements a very general tree structure - a node may have any number of children.