home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR502.DOS / SOURCE / SAMPLE / STACK.PRG < prev    next >
Encoding:
Text File  |  1993-02-15  |  3.7 KB  |  163 lines

  1. /***
  2. *
  3. *  Stack.prg
  4. *
  5. *  Functions to implement a stack data type
  6. *
  7. *  Copyright (c) 1993, Computer Associates International Inc.
  8. *  All rights reserved.
  9. *
  10. *  NOTE: Compile with /a /m /n /w
  11. *
  12. */
  13.  
  14.  
  15. /***
  16. *
  17. *    What Is a Stack?
  18. *    A stack is a common Last-In-First-Out (LIFO) data structure.
  19. *    An analogy would be a stack of books on a table. If you
  20. *    place Book A on the table, then place Book B on top of Book
  21. *    A, then place Book C on top of Book B, you have created a
  22. *    stack with three members. Book C is the "top" of the stack;
  23. *    Book A is the "bottom" of the stack.
  24. *
  25. *    Adding a new item to a stack is referred to as "pushing"
  26. *    the item onto the stack. Thus we have "pushed" three items
  27. *    onto our stack of books.  Removing the top item of the
  28. *    stack is called "popping" the item. Unlike a stack of books,
  29. *    you can't pull something out of the middle of a stack data
  30. *    structure -- the items are always popped in reverse order.
  31. *    That is, the last item in is the first item out (LIFO).
  32. *
  33. *    Using the functions in this file, we could model our stack
  34. *    of books like this:
  35. *
  36. *    // Create an empty stack
  37. *    aStack := StackNew()
  38. *    // "Push" each item onto the stack
  39. *    StackPush( aStack, "Book A" )
  40. *    StackPush( aStack, "Book B" )
  41. *    StackPush( aStack, "Book C" )
  42. *
  43. *    // Now "pop" them off
  44. *    ? StackPop( aStack )      // Prints "Book C"
  45. *    ? StackPop( aStack )      // Prints "Book B"
  46. *    ? StackPop( aStack )      // Prints "Book A" (the stack is now empty)
  47. *
  48. *
  49. *    A real example might be a stack of color settings:
  50. *
  51. *    aColors := StackNew()
  52. *
  53. *    StackPush( aColors, SETCOLOR() )  // Save current color setting
  54. *    SETCOLOR( ... )                   // Change color setting
  55. *
  56. *    ...                               // Routine's code here
  57. *
  58. *    SETCOLOR( StackPop( aColors ) )   // Restore color on way out
  59. *
  60. *
  61. *
  62. *  This implementation involves the following functions:
  63. *
  64. *    StackNew() --> aStack
  65. *    Create a new stack
  66. *
  67. *    StackPush( <aStack>, <exp> ) --> aStack
  68. *    Push a new value onto the stack
  69. *
  70. *    StackPop( <aStack> ) --> xValue
  71. *    Pop a value from the stack, return NIL is stack is empty
  72. *
  73. *    StackIsEmpty( <aStack> ) --> lEmpty
  74. *    Determine if a stack has no members
  75. *
  76. *    StackTop( <aStack> ) --> xValue
  77. *    Return top stack member without removing from stack
  78. *
  79. */
  80.  
  81.  
  82. /***
  83. *
  84. *   StackNew() --> aStack
  85. *
  86. *   Create a new stack
  87. *
  88. */
  89. FUNCTION StackNew()
  90.    RETURN ( {} )     // Return an empty array
  91.  
  92.  
  93.  
  94. /***
  95. *
  96. *   StackPush( <aStack>, <xValue> ) --> aStack
  97. *
  98. *   Push a new value onto the stack
  99. *
  100. */
  101. FUNCTION StackPush( aStack, xVal )
  102.    
  103.    // Add new element to the stack array and then return the array
  104.    RETURN ( AADD( aStack, xVal ) )
  105.  
  106.  
  107.  
  108. /***
  109. *
  110. *   StackPop( <aStack> ) --> xValue
  111. *
  112. *   Pop a value from the stack
  113. *
  114. *   NOTE: Returns NIL if nothing is on the stack
  115. *
  116. */
  117. FUNCTION StackPop( aStack )
  118.    
  119.    LOCAL xValueLast
  120.    LOCAL nLen := LEN( aStack )
  121.  
  122.    // Check for underflow condition
  123.    IF nLen == 0
  124.       RETURN ( NIL )       // NOTE
  125.    ENDIF
  126.  
  127.    // Get the last element value
  128.    xValueLast := aStack[ nLen ]
  129.  
  130.    // Remove the last element by shrinking the stack
  131.    ASIZE( aStack, nLen - 1 )
  132.  
  133.    // Return the last element's value
  134.    RETURN ( xValueLast )
  135.  
  136.  
  137.  
  138. /***
  139. *
  140. *  StackIsEmpty( <aStack> ) --> lEmpty
  141. *
  142. *  Determine if a stack has no members
  143. *
  144. */
  145. FUNCTION StackIsEmpty( aStack )
  146.    RETURN ( EMPTY( aStack ) )
  147.  
  148.  
  149.  
  150. /***
  151. *
  152. *  StackTop( <aStack> ) --> xValue
  153. *
  154. *  Retrieve top stack member without removing
  155. *
  156. */
  157. FUNCTION StackTop( aStack )
  158.    
  159.    // Return the value of the last element in the stack array
  160.    RETURN ( ATAIL( aStack ) )
  161.