structure v0.2.2 Structure.Stack View Source

A simple stack data structure. Supports push, pop and head operations.

Link to this section Summary

Functions

Checks if a given stack is empty

Creates a stack from a list

Returns the head of the stack

Returns a new empty stack

Pops top element from stack Returns {:ok, stack} for success, or {:error, :empty_stack} if the stack was empty

Pushes an item on the stack

Returns the size of a stack

Link to this section Types

Link to this type head_response() View Source
head_response() :: {:ok, any} | {:error, atom}
Link to this type pop_response() View Source
pop_response() :: {:ok, stack} | {:error, atom}

Link to this section Functions

Link to this function empty?(stack) View Source
empty?(stack) :: boolean

Checks if a given stack is empty

Examples

iex> Structure.Stack.empty?(%Structure.Stack{size: 0, list: []})
true
Link to this function from_list(list) View Source
from_list([any]) :: stack

Creates a stack from a list.

Returns the head of the stack.

It should return {:ok, head}.

If the stack is empty, it returns {:error, :empty_stack} instead.

Examples

iex> Structure.Stack.head(%Structure.Stack{size: 0, list: []})
{:error, :empty_stack}

Returns a new empty stack.

Pops top element from stack Returns {:ok, stack} for success, or {:error, :empty_stack} if the stack was empty.

Link to this function push(stack, item) View Source
push(stack, any) :: stack

Pushes an item on the stack.

Link to this function size(stack) View Source
size(stack) :: non_neg_integer

Returns the size of a stack.