home *** CD-ROM | disk | FTP | other *** search
- /***
- * Stack.prg
- * Functions to implement stacks.
- * Copyright (c) 1990 Nantucket Corp. All rights reserved.
- *
- * NOTE: compile with /n/w/a/m
- */
-
- /***
- * What Is a Stack?
- *
- * A stack is a common Last-In-First-Out (LIFO) data structure.
- * An analogy would be a stack of books on a table. If you
- * place Book A on the table, then place Book B on top of Book
- * A, then place Book C on top of Book B, you have created a
- * stack with three members. Book C is the "top" of the stack;
- * Book A is the "bottom" of the stack.
- *
- * Adding a new item to a stack is referred to as "pushing"
- * the item onto the stack. Thus we have "pushed" three items
- * onto our stack of books. Removing the top item of the
- * stack is called "popping" the item. Unlike a stack of books,
- * you can't pull something out of the middle of a stack data
- * structure -- the items are always popped in reverse order.
- * That is, the last item in is the first item out (LIFO).
- *
- * Using the functions in this file, we could model our stack
- * of books like this:
- *
- * // Create an empty stack
- * aStack := StackNew()
- *
- * // "Push" each item onto the stack
- * StackPush( aStack, "Book A" )
- * StackPush( aStack, "Book B" )
- * StackPush( aStack, "Book C" )
- *
- * // Now "pop" them off
- * ? StackPop( aStack ) // Prints "Book C"
- * ? StackPop( aStack ) // Prints "Book B"
- * ? StackPop( aStack ) // Prints "Book A" (the stack is now empty)
- *
- *
- * A real example might be a stack of color settings:
- *
- * aColors := StackNew()
- *
- * StackPush( aColors, SETCOLOR() ) // Save current color setting
- * SETCOLOR( .... ) // Change color setting
- * .... // Call other functions doing same
- * // thing
- *
- * SETCOLOR( StackPop( aColors ) ) // Restore color on way out
- *
- */
-
-
- /***
- * Functions:
- *
- * StackNew() --> aStack
- * Create a new stack
- *
- * StackPush( <aStack>, <exp> ) --> aStack
- * Push a new value onto the stack
- *
- * StackPop( <aStack> ) --> value
- * Pop a value from the stack, return NIL is stack is empty
- *
- * StackIsEmpty( <aStack> ) --> lEmpty
- * Determine if a stack has no members
- *
- * StackTop( <aStack> ) --> value
- * Return top stack member without removing from stack
- *
- */
-
-
- /***
- * StackNew() --> aStack
- * Create a new stack
- */
- FUNCTION StackNew()
- RETURN {}
-
-
- /***
- * StackPush( <aStack>, <exp> ) --> aStack
- * Push a new value onto the stack
- */
- FUNCTION StackPush( aStack, exp )
- // Add new element to the stack array and then return the array
- RETURN AADD( aStack, exp )
-
-
- /***
- * StackPop( <aStack> ) --> value
- * Pop a value from the stack
- *
- * Return NIL if nothing is on the stack.
- */
- FUNCTION StackPop( aStack )
- LOCAL valueLast, nLen := LEN( aStack )
-
- // Check for underflow condition
- IF nLen == 0
- RETURN NIL
- ENDIF
-
- // Get the last element value
- valueLast := aStack[ nLen ]
-
- // Remove the last element by shrinking the stack
- ASIZE( aStack, nLen - 1 )
-
- // Return the last element's value
- RETURN valueLast
-
-
- /***
- * StackIsEmpty( <aStack> ) --> lEmpty
- * Determine if a stack has no members
- *
- */
- FUNCTION StackIsEmpty( aStack )
- RETURN EMPTY( aStack )
-
-
- /***
- * StackGetTop( <aStack> ) --> value
- * Retrieve top stack member without removing
- *
- */
- FUNCTION StackGetTop( aStack )
- //
- // Return the value of the last element in the stack array
- RETURN ATAIL( aStack )
-
-
- /***
- * StackTop( <aStack> ) --> value
- * Retrieve top stack member without removing
- *
- */
- FUNCTION StackTop( aStack )
- //
- // Return the value of the last element in the stack array
- RETURN ATAIL( aStack )
-