Stack.st
author Claus Gittinger <cg@exept.de>
Wed, 11 Jun 2003 10:37:18 +0200
changeset 1245 99e070f7c351
parent 1222 19e99db6427d
child 1311 e20fa06ce5f8
permissions -rw-r--r--
comments

"{ Package: 'stx:libbasic2' }"

OrderedCollection subclass:#Stack
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Ordered'
!

!Stack class methodsFor:'documentation'!

documentation
"
    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)

    [author:]
        Stefan Vogel

    [see also:]
        OrderedCollection Queue
"
!

examples
"
  push-push-....
                                                                [exBegin]
    |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.
                                                                [exEnd]

  popping too many:
                                                                [exBegin]
    |aStack|

    aStack := Stack new.
    aStack push:1.

    aStack pop.
    aStack pop.
                                                                [exEnd]
"
! !

!Stack class methodsFor:'instance creation'!

new

    ^ self new:100
! !

!Stack methodsFor:'accessing'!

pop
    "Answer the object on top of the stack."

    ^ self removeLast
!

pop: numElem 
    "Pop and discard top numElems and answer the receiver.
     Caveat: pop: is a misleading name; should propably be called drop:"

    self removeLast:numElem
!

push: anObject 
    "Push anObject onto the top of the stack."

    ^ self add:anObject
!

top
    "Answer (without removing) the object on top of the stack."

    ^ self last
! !

!Stack methodsFor:'enumerating'!

do: aBlock
    "Evaluate aBlock for each object on the stack, from top to bottom."

    ^ super reverseDo:aBlock
!

reverseDo: aBlock
    "Evaluate aBlock for each object on the stack, from bottom to top."

    ^ super do:aBlock
! !

!Stack class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/Stack.st,v 1.6 2003-06-11 08:36:55 cg Exp $'
! !