Top Navigational Bar

Stacks: what they are and how to implement them
DocumentID: 20182
Revision Date: 22-Oct-96

The information in this document applies to:
WordPerfect® 5.1 for DOS

Problem

Keywords: QUEUE LIFO FIFO BUFFER RPN
When manipulating data, there are many times that the amount of data will
not be known in advance. A good example of such a collection of data is
the Paradox table, which can store up to 256MB of information.

However, tables can be clumsy to work with for small amounts of data, and
are slower than accessing memory variables.

For example, suppose that you're building a help system with multiple
choices for additional help, and you want to allow the user to back up each
level, one at a time. In other words, you want the last screen the user
accessed to be the first screen they see when they back up.

This kind of Last In First Out is also called a stack. It's fairly easy to
implement using a table -- just keep adding and deleting records at the
end of the table.

It's also fairly easy -- and a lot faster -- to use arrays to do the same
trick. Arrays are also better because they can contain elements of
different types: strings, numbers, and dates.

There are two methods (three if you use Paradox for Windows' resizable
arrays, but that won't be covered here):

* The simple way (if you have 4.0 or higher) is to use a dynarray. To
create the stack just declare the dynarray. Adding and deleting elements is
usually called "pushing" and "popping", so here are a couple of PROCs for
it:

PROC Push ( Stack, Item )
Stack [ DYNARRAYSIZE(Stack) + 1 ] = Item
ENDPROC

PROC Pop ( Stack )
PRIVATE Item, StackSize
StackSize = DYNARRAYSIZE(Stack)
IF StackSize = 0 THEN
RETURN "Error: no stack elements"
ENDIF
Item = Stack [ Stacksize ]
RELEASE VARS Stack [ StackSize ]
RETURN Item
ENDPROC

These two PROCs make use of the fact that DYNARRAYSIZE() is always going to
point to the top of the stack, the element we want to use.

* For 3.5 and earlier, you need to use a fixed array, which limits the
maximum number of items in the stack. Here's some pseudo-code which
demonstrates a 20-item stack:

StackSize = 20
ARRAY Stack[StackSize]
TopOfStack = 0

; Push an item
IF TopOfStack < StackSize THEN
TopOfStack = TopOfStack + 1
Stack [ TopOfStack ] = Item
ENDIF

; Pop an item
IF TopOfStack > 0 THEN
Item = Stack [ TopOfStack ]
RELEASE VARS Stack[TopOfStack] ; Not really necessary for a small
; stack, but a good idea if
elements
; may be memos
TopOfStack = TopOfStack - 1
ENDIF

BERNDA01 TPDX4686 TRN 08/02/93 All MonData

Answer:

Details:


Product specifications, packaging, technical support and information (*Specifications*) refer to theUnited States retail English version only. Specifications for other versions may vary. All Specifications, claims, features, representations, and/or comparisons provided are correct to the best of our knowledge of the date of publication, but are subject to change without notice.OUR MAXIMUM AGGREGATE LIABILITY TO YOU AND THAT OF OUR DEALERS AND SUPPLIERS IS LIMITED. IT SHALL NOTEXCEED THE AMOUNT YOU PAID TO ACCESS THE INFORMATION. SEE LEGAL DISCLAIMER.