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?

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
parsifal
  • 879
  • 4
  • 12
  • 21
  • I've attempted to improve the English of your question. If I've changed what you meant to say, please correct me. – Frank Shearar Feb 22 '11 at 08: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 '11 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 '11 at 02:35

1 Answers1

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.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
  • 1
    "pointer arithmetic" is the term for what Smalltalk and Java are "missing". – Joachim Sauer Feb 22 '11 at 08:48
  • 3
    "Missing" here means "makes no sense in this language" rather than "gosh we forgot". – Frank Shearar Feb 22 '11 at 09: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 '11 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 http://www.c2.com/cgi/wiki?TypelessVsDynamic for instance. – Frank Shearar Feb 22 '11 at 10:26