TOP | UP: Data Manipulation | NEXT: Extending the Language |
A stack is a last-in-first-out storage structure, like a pile of plates: one can put a new plate on top at any time, and whenever one reaches to retrieve a plate from the top of the pile it is always the one most recently added to the pile. UserTalk implements stacks with utility scripts; they are at suites.stacks , and are worth studying, as they might give you ideas for implementing similar storage structures, such as queues.
addrMyStack = stack.create ()
The variable on the left of the assignment can be anything; the point is to capture the value of stack.create(), because this is the address of your new stack, and is needed as a parameter to the other stack verbs.
stack.push (addrMyStack, theValue)
To remove a value from a stack:
stack.pop (addrMyStack)
The most recently pushed value is removed from the stack, and returned as the value of the verb call. If the stack is empty, a runtime error is generated; you can handle this gracefully by embedding the call in a try...else structure.
To learn the number of elements on a stack, get its top. For instance, if we have captured the stack's address as addrMyStack, then addrMyStack^.top is the number of elements of the stack.
To examine the n th element on a stack, where the first element is the one first pushed onto the stack, say this:
addrMyStack.vals.["item" + n]
When you are done using a stack, you should dispose of it or it will remain in the database. Simply say:
stack.dispose (addrmyStack)
The verb stack.visit() is discussed Chapter 12, Control Structures.
TOP | UP: Data Manipulation | NEXT: Extending the Language |