## `adt::Stack` -- abstract data type ``Stack''

#### Introduction

`adt::Stack` implements the abstract data type ``Stack''.

#### Call(s)

`adt::Stack(stack)`

#### Parameters

`stack` | - | an expression sequence of objects to initialize the stack

#### Returns

an object of the domain `adt::Stack`

#### Details

- `adt::Stack` implements the abstract data type ``Stack''. To create a stack, an expression sequence of any MuPAD objects can be given to initialize the stack, otherwise an empty stack is builded.

- The methods of all abstract datatypes must be called especially and will result changing the object itself as sideeffect.

- With `S := adt::Stack()` an empty stack is builded and assigned to the variable `S`.
- Every stack will be displayed as `Stack` followed by a number. This name is generated by `genident`.

- All following methods changes the value of `S` itself. A new assignment to the variable (in this example `S`) is not necessary, in contrast to all other MuPAD functions and data types.

- The methods `depth`, `empty`, `pop`, `push`, `reverse` and `top` are available for handling with stacks.

- #### Method S::depth: depth of the stack

  - `S::depth()`
  - - This method returns the depth of the stack, that is the number of elements in the stack.

  #### Method S::empty: is the stack empty

  - `S::empty()`
  - - This method returns `TRUE`, if no element is in the stack, otherwise `FALSE`.

  #### Method S::pop: fill up the stack

  - `S::pop(x)`
  - - This method puts the element `x` at the top of the stack.

  #### Method S::push: get an element from the stack

  - `S::push()`
  - - This method returns the top element of the stack (the last added) with removing it from the stack.

  #### Method S::reverse: revert the stack

  - `S::reverse()`
  - - This method reverses the order of all elements in the stack.

  #### Method S::top: top of the stack

  - `S::top()`
  - - This method returns the top element of the stack (the last added) without removing it from the stack.

#### Example 1

Create a new stack with strings as arguments.

    >> S := adt::Stack("1", "2", "3", "4")

                                      Stack1

Show the length of the stack.

    >> S::depth()

                                         4

Fill up the stack with a new element. The stack will be changed by the method, no new assignment to `S` is necessary!

    >> S::push("5")

                                        "5"

Show the top of the stack. This method does not change the stack.

    >> S::top(), S::top()

                                     "5", "5"

After twice getting an element of the stack, the third element is the new top of the stack, and the length is `3`.

    >> S::pop(), S::pop(), S::top(), S::depth()

                                 "5", "4", "3", 3

Now revert the stack. The last element will be the first element.

    >> S::reverse(): S::top()

                                        "1"

Fill up the stack with `"0"`.

    >> S::push("0"):
       S::empty()

                                       FALSE

Finally collect all elements of the stack in the list assigned to `ARGS`, until the stack is empty.

    >> ARGS := []: while not S::empty() do ARGS := append(ARGS, S::pop()) end:
       ARGS

                               ["0", "1", "2", "3"]

#### Changes

- new function

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000