SimpleStack - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

# Online Help

###### All Products    Maple    MapleSim

SimpleStack

The Basic Stack Constructor

 Calling Sequence SimpleStack() MeteredStack() BoundedStack(bound::posint) type(e::anything, 'Stack') s:-push(e::anything) s:-pop() s:-empty() s:-top() s:-depth() s:-init()   $include Push(s::Stack, e::anything) Pop(s::Stack) Top(s::Stack) EmptyP(s::Stack) Parameters  s - stack returned by SimpleStack e - arbitrary Maple expression (not an expression sequence) bound - positive integer bounding the maximums size of the stack Description  • The procedure SimpleStack is a stack constructor. It returns a Maple expression that implements a stack object, which is of type Stack.  • You can test whether a Maple expression e is a Stack object by using type( e, 'Stack' ). An expression is of type Stack if it is an object with the methods empty, top, push, and pop. Specific Stack implementations may support additional methods, but all Stacks support at least these four methods. Stacks built by the constructor SimpleStack are currently represented by modules, so the message passing syntax uses the :- operator.  • The empty method returns the value true if no items are on the stack, and returns the value false otherwise.  • To insert an item e (any Maple expression) on the top of a stack, use the push method. The pushed value is returned.  • Items may be removed from the top of the stack by using the pop method. An error is raised if the stack is empty. This error may be caught using the exception string "empty stack".  • The item on the top of a non-empty stack may be examined, without changing the contents of the stack, by using the method top. If the stack is non-empty, this method returns the item on the top of the stack (that value which will be returned by the next call to the pop method), and otherwise raises the "empty stack" exception.  • Stacks constructed by the SimpleStack constructor also support the methods depth, which returns the number of items on the stack, and init, which initializes the stack (and empties it).  • The standard include file defines several inline procedures for invoking the basic Stack operations. The procedures provided are Enqueue, Dequeue, EmptyP, and Front. These procedures are not part of the Maple library, and are provided only as inlined procedures. (Note: The include file $⟨\mathrm{Queue}·\mathrm{mi}⟩$ also provides an EmptyP inlined procedure, but it is compatible with the one in , so both include files may be used in the same Maple source file.)  • The BoundedStack constructor returns a bounded stack; that is, a stack whose maximum depth is bounded by a predetermined positive integer bound specified in the call to the constructor. Bounded stacks export the additional method full, which returns the value true if the stack is full (no more items can be pushed), and the value false otherwise.  • A stack which records the numbers of pushes, pops and the maximum stack depth achieved can be created using the MeteredStack constructor. It returns a stack that exports the additional method stats, which returns an expression sequence of three non-negative integers. The first is the total number of pushes effected since the stack was initialized. The second is the total number of pops since the most recent initialization. The third integer in the sequence is the maximum stack depth achieved since the last time the init method was called. The init method for a metered stack resets these counters to $0$. Metered stacks are intended to be used for performance monitoring; the extra overhead of tracking the stack statistics imposes a noticeable performance penalty. A useful technique is to include code like the following in your Maple source code.  >$include      $ifdef _DEBUG_ macro( Stack = MeteredStack ):$else  macro( Stack = SimpleStack ):  \$endif
 • For a non-object-oriented stack implementation, see the stack package.

Examples

 > $S≔\mathrm{SimpleStack}\left(\right):$
 > $\mathrm{type}\left(S,'\mathrm{Stack}'\right)$
 ${\mathrm{true}}$ (1)
 > $S:-\mathrm{push}\left(1\right);$$S:-\mathrm{push}\left(2\right);$$S:-\mathrm{push}\left(3\right)$
 ${1}$
 ${2}$
 ${3}$ (2)
 > $S:-\mathrm{depth}\left(\right)$
 ${3}$ (3)
 > $S:-\mathrm{top}\left(\right)$
 ${3}$ (4)
 > $S:-\mathrm{pop}\left(\right)$
 ${3}$ (5)
 > $S:-\mathrm{top}\left(\right)$
 ${2}$ (6)
 > $\mathbf{while}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{not}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}S:-\mathrm{empty}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}S:-\mathrm{pop}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}$
 ${1}$ (7)
 > $S:-\mathrm{depth}\left(\right)$
 ${0}$ (8)
 > $S:-\mathrm{top}\left(\right)$
 > $\mathrm{MathFolks}≔\mathrm{MeteredStack}\left(\right):$
 > $\mathrm{MathFolks}:-\mathrm{push}\left("Alex"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Allan"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Dave"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("David"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Edgardo"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("James"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Juergen"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Laurent"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Michael"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Paulina"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Raqeeb"\right):$$\mathrm{MathFolks}:-\mathrm{push}\left("Stephen"\right):$$\mathbf{while}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{not}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{MathFolks}:-\mathrm{empty}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{MathFolks}:-\mathrm{pop}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}$
 ${"Alex"}$ (9)
 > $\mathrm{MathFolks}:-\mathrm{stats}\left(\right)$
 ${12}{,}{12}{,}{12}$ (10)
 > $B≔\mathrm{BoundedStack}\left(10\right):$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}10\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}B:-\mathrm{push}\left(i\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$$B:-\mathrm{push}\left(\mathrm{one_more}\right)$

 See Also