Skip to main content

Stack

Class Stack<X> provides a Minimal LIFO stack of elements of type X.

See library Deque for mixed LIFO/FIFO behavior.

Example:

import Stack "mo:base/Stack";

let stack = Stack.Stack<Nat>(); // create a stack

Runtime: O(1)

Space: O(1)

Class Stack<T>

class Stack<T>()

Function push

func push(x : T)

Push an element on the top of the stack.

Example:

stack.push(1);
stack.push(2);
stack.push(3);
stack.peek(); // examine the top most element

Runtime: O(1)

Space: O(1)

Function isEmpty

func isEmpty() : Bool

True when the stack is empty and false otherwise.

Example:

stack.isEmpty();

Runtime: O(1)

Space: O(1)

Function peek

func peek() : ?T

Return (without removing) the top element, or return null if the stack is empty.

Example:

stack.push(1);
stack.push(2);
stack.push(3);
stack.peek();

Runtime: O(1)

Space: O(1)

Function pop

func pop() : ?T

Remove and return the top element, or return null if the stack is empty.

Example:

stack.push(1);
ignore stack.pop();
stack.isEmpty();

Runtime: O(1)

Space: O(1)