如果Squeak不是脚本语言,那么树形数据结构在哪里?

发布于 2024-10-18 15:58:59 字数 811 浏览 5 评论 0原文

因为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-10-25 15:58:59

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 that myVar is a pointer pointing to an empty OrderedCollection.

Yes, you can simulate a stack using OrderedCollection by using addFirst: to push elements and removeFirst: and removeFirst to pop elements. (Similarly, you can simulate a queue by pushing elements with addFirst: and removing them with removeLast.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文