|
Class: Stack
Object
|
+--Collection
|
+--SequenceableCollection
|
+--OrderedCollection
|
+--Stack
- Package:
- stx:libbasic2
- Category:
- Collections-Ordered
- Version:
- rev:
1.11
date: 2019/04/03 10:42:17
- user: cg
- file: Stack.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Stefan Vogel
A simple implementation of a Stack.
Notice, this is simply syntactic sugar - all functionality is
already provided by the OrderedCollection class
(addLast <==> push / removeLast <==> pop / last <==> top)
OrderedCollection
Queue
instance creation
-
new
-
accessing
-
drop: n
-
remove n elements from the top of the stack.
-
pop
-
Answer the object on top of the stack.
-
pop: numElem
-
Pop and discard top numElems and answer the receiver.
Caveat: pop: is a misleading name; should propably be called drop:
-
push: anObject
-
Push anObject onto the top of the stack.
-
top
-
Answer (without removing) the object on top of the stack.
enumerating
-
do: aBlock
-
Evaluate aBlock for each object on the stack, from top to bottom.
-
reverseDo: aBlock
-
Evaluate aBlock for each object on the stack, from bottom to top.
push-push-....
|aStack|
aStack := Stack new.
Transcript showCR:aStack.
Transcript showCR:'push 1: '.
aStack push:1.
Transcript showCR:'push 2: '.
aStack push:2.
Transcript showCR:'push 3: '.
aStack push:3.
Transcript showCR:aStack.
Transcript show:'pop: '; showCR:(aStack pop).
Transcript show:'pop: '; showCR:(aStack pop).
Transcript show:'pop: '; showCR:(aStack pop).
Transcript showCR:aStack.
|
popping too many:
|aStack|
aStack := Stack new.
aStack push:1.
aStack pop.
aStack pop.
|
|