# "Stack"(Data Structure)

"Stack"

represents a stack of expressions.

# Details

• A stack is a collection of elements that supports last-in, first-out insertion and removal:
•  CreateDataStructure["Stack"] create a new empty "Stack" CreateDataStructure["Stack",elems] create a new "Stack" containing elems Typed[x,"Stack"] give x the type "Stack"
• For a data structure of type "Stack", the following operations can be used:
•  ds["Copy"] return a copy of ds time: O(n) ds["DropAll"] drop all the elements from ds time: O(n) ds["Elements"] return a list of the elements of ds time: O(n) ds["EmptyQ"] True, if the ds is empty time: O(1) ds["Fold",fun,init] apply fun to the elements of ds, starting with init, accumulating a result time: O(n) ds["Length"] number of elements in ds time: O(1) ds["Peek"] last element in ds time: O(1) ds["Pop"] remove the last element in ds and return it time: O(1) ds["Push",x] add x to the end of ds time: O(1) ds["PushList",elems] add elems to the end of ds time: O(nelems) ds["Visualization"] return a visualization of ds time: O(n)
• The following functions are also supported:
•  dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Normal[ds] convert ds to a normal expression

# Examples

open allclose all

## Basic Examples(2)

A new "Stack" can be created with CreateDataStructure:

ds is initially empty:

ds is no longer empty:

It has one element:

Add another element and peek. This shows the last element:

Remove the last element and return it:

Return an expression version of ds:

It is fast to push elements onto a stack:

A visualization of the data structure can be generated:

Sum all elements:

## Scope(1)

### Information(1)

A new "Stack" can be created with CreateDataStructure:

Information about the data structure ds: