home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / rexx / stack.cmd < prev    next >
OS/2 REXX Batch file  |  1999-05-11  |  2KB  |  47 lines

  1. /******************************************************************************/
  2. /*  stack                    Object REXX Samples                              */
  3. /*                                                                            */
  4. /*  A stack class                                                             */
  5. /*                                                                            */
  6. /*  This program demonstrates how to implement a stack class using ::class    */
  7. /*  and ::method directives.  Also included is a short example of the use of  */
  8. /*  a stack.                                                                  */
  9. /******************************************************************************/
  10.  
  11. mystack = .stack~new                        /* Create a new instance of stack */
  12. mystack~push('John Smith')
  13. mystack~push('Bill Brown')
  14. say 'Size of stack:' mystack~items          /* Displays size of stack         */
  15. say 'Pop stack:    ' mystack~pop            /* Displays item popped off stack */
  16. say 'Pop stack:    ' mystack~pop            /* Displays next item popped off  */
  17. say 'Pop stack:    ' mystack~pop            /* Stack now empty - displays     */
  18.                                             /*   "The NIL Object"             */
  19.  
  20. /* Define the stack class (a subclass of Object Class) */
  21. ::class stack
  22.  
  23. /* Define the init method on the stack class */
  24. ::method init
  25. expose contents                             /* Object var with stack contents */
  26. self~init:super                             /* Run init method of superclass  */
  27. contents = .list~new                        /* Use a list to keep stack items */
  28.  
  29. /* Define the push method on the stack class */
  30. ::method push
  31. expose contents
  32. use arg item
  33. contents~insert(item, .nil)
  34.  
  35. /* Define the pop method on the stack class */
  36. ::method pop
  37. expose contents
  38. if contents~items > 0 then
  39.   return contents~remove(contents~first)
  40. else
  41.   return .nil
  42.  
  43. /* Define the items method on the stack class to return number of items */
  44. ::method items
  45. expose contents
  46. return contents~items
  47.