1

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?

3
  • I've attempted to improve the English of your question. If I've changed what you meant to say, please correct me. Feb 22, 2011 at 8:45
  • I don't see why you make the relation between scripting language and tree data structure. Btw "scripting language" is a non-expression IMHO.
    – mathk
    Feb 24, 2011 at 17:53
  • Scripting languages like perl, Tcl represend some kinds of glue, like pipe in unix, but they are not suitable for use in algorithms area, however, small talk as an OO language, it should implement algoritms, data structure etc. That's the opionion, frind :)
    – parsifal
    Feb 28, 2011 at 2:35

1 Answer 1

4

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.

4
  • 1
    "pointer arithmetic" is the term for what Smalltalk and Java are "missing". Feb 22, 2011 at 8:48
  • 3
    "Missing" here means "makes no sense in this language" rather than "gosh we forgot". Feb 22, 2011 at 9:38
  • 1
    I have viewd your ZTree code, it sounds good:) thanks your answer, and I gonner to know small talk is a typeless language, modern computer language except script language have types, May be typeless is one of the reason why smalltalk cannot be as popular as java,c++, etc.
    – parsifal
    Feb 22, 2011 at 10:03
  • 4
    It's not a typeless language. Variables have no type, but values are strongly typed (in the sense that you may not subvert the type system). See c2.com/cgi/wiki?TypelessVsDynamic for instance. Feb 22, 2011 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.