home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a009 / 6.ddi / SAMPLE.LIF / STACK.PRG < prev    next >
Encoding:
Text File  |  1991-04-14  |  3.8 KB  |  150 lines

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