{######################################################################### # STACK IMPLEMENTATION (first-in last-out) +------+ # USING LISTMOD (LIST.PAS) top--> | e3 | # +------+ # by DAVE HEYLIGER - AMUS STAFF | e2 | # +------+ # | e1 | # NOTE: 1) This module employs the LIST +------+ # implementation, therefore, a //////|\\\\\\\ # copy of LIST.PAS is required. # 2) For the Driver Program, a file called STACK.EXT is necessary. # This will define all necessary external func/proc of this # module, i.e. : external procedure stackmakenull..... # external procedure stackpush........ # | | | # 3) For this module, an INCLUDE file called STACK.TYP must be copied # before compilation. This file includes all TYPE declarations. Use # this file in your driver program also. It is in this file that # the user can modify the stackelement. If you modify stackelement, # be sure to modify PRINT and COMPARE in LIST.TYP # 4) In order to employ the LIST implementation within this module, # you also must have LIST.EXT that defines all list func/procs. # 5) Final linkage includes your Driver, STACK, and LIST. # # LAST UPDATE: 08/20/85 # ############################################################################} MODULE STACKMOD; {+-- This module is the implementation of a stack adt. | Operations provided are: | | stackmakenull - initialize stack to empty stack | | stackpush - push element on stack | | stacktop - return top element fo stack | | stackpop - pop off top element from stack | | stackempty - checks for empty stack | | stackprint - lists contents of stack from top to bottom | +----------------------------------------------------------------------} {$I stack.typ} {$I list.ext} procedure stackmakenull(var s: stack); {+--- on entry - s is a stack variable | on exit - stack s initialized and emptied +------------------------------------------------} var p: listposition; begin { stackmakenull } p := listmakenull(s); end; { stackmakenull } procedure stackpush(x: stackelement; var s: stack); {+--- on entry - s was previously initialized | on exit - x is pushed on the stack +----------------------------------------------------} begin { stackpush } listinsert(x,listfirst(s),s); end; { stackpush } function stacktop(s: stack): stackelement; {+--- on entry - s is a non empty stack | on exit - returns top of stack element +-----------------------------------------------------} begin { stacktop } stacktop := listretrieve(listfirst(s),s); end; { stacktop } procedure stackpop(var s:stack); {+--- on entry - s is non empty stack | on exit - top of stack is removed +------------------------------------------------------} begin { stackpop } listdelete(listfirst(s),s); end; { stackpop } function stackempty(s: stack): boolean; {+--- on entry - s is a previously initialized stack variable | on exit - returns true iff stack empty otherwise false +-------------------------------------------------------------} begin { stackempty } stackempty := listfirst(s) = listend(s); end; { stackempty } procedure stackprint(s: stack; var out: text); {+--- on entry - s is a previously initialized stack variable | on exit - contents of stack s is printed top to bottom on file out | in a readable form +------------------------------------------------------------------------} begin { stackprint } listprint(s,output); end; { stackprint } . .