The following classes implement a simple stack based on the Array class. When items are pushed onto the stack, the size of the array is checked to see if the item will fit. If not, the add method( ) is used. When items are popped from the stack, the array does not shrink. If another item is pushed onto the stack, the item can be stored in an existing array element.

class Stack of Array

   this.ptr = 0 

   

function push( xArg ) 

   if this.ptr == this.size 

      this.add( xArg ) 

      this.ptr++ 

   else 

      this[ ++this.ptr ] = xArg 

   endif 

   

function pop( )  

   if this.ptr > 0 

      return this[ this.ptr-- ] 

   else 

      throw new StackException( ) 

   endif 

   

function top( ) 

   if this.ptr > 0 

      return this[ this.ptr ] 

   else 

      throw new StackException( ) 

   endif 

   

function empty( ) 

  return this.ptr == 0 

endclass

class StackException of Exception

   this.message = "Stack fault" 

endclass