home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 March / VPR9803A.ISO / Netscape / NAV40.Z / Stack.js < prev    next >
Text File  |  1997-08-14  |  1KB  |  75 lines

  1. /* ==================================================================
  2. FILE:   Stack.js
  3. DESCR:  Stack object.
  4. NOTES:  
  5. ================================================================== */
  6.  
  7. /*
  8. DESCR:   Stack class.
  9. PARAMS:
  10. RETURNS:
  11. NOTES:
  12. */
  13. function stack()
  14. {
  15.    this.pointer = -1
  16.    this.topItem
  17.    this.aItems = new Array
  18.  
  19.    this.push    = push
  20.    this.pop     = pop
  21.    this.isEmpty = isEmpty
  22.    this.clear   = clear
  23. }
  24.  
  25.    /*
  26.    DESCR:   
  27.    PARAMS:  item  A variable of any type.
  28.    RETURNS: 
  29.    NOTES:   
  30.    */
  31.    function push( item )
  32.    {
  33.       this.topItem = item
  34.       this.aItems[ ++this.pointer ] = item
  35.    }
  36.  
  37.    /*
  38.    DESCR:
  39.    PARAMS:
  40.    RETURNS: The item.
  41.    NOTES:
  42.    */
  43.    function pop()
  44.    {
  45.       if ( this.isEmpty() ) return null
  46.       this.topItem = this.aItems[ this.pointer - 1 ]
  47.       return this.aItems[ this.pointer-- ]
  48.    }
  49.  
  50.    /*
  51.    DESCR:   Empties the stack.
  52.    PARAMS:
  53.    RETURNS:
  54.    NOTES:
  55.    */
  56.    function clear()
  57.    {
  58.       this.pointer = -1
  59.       this.topItem = null
  60.    }
  61.  
  62.    /*
  63.    DESCR:
  64.    PARAMS:
  65.    RETURNS: true if the stack contains no members; false otherwise.
  66.    NOTES:
  67.    */
  68.    function isEmpty()
  69.    {
  70.       return ( this.pointer < 0 ? true : false )
  71.    }
  72.  
  73. // End class definition: stack.
  74.  
  75.