calc v1.0.0 Stack

A Stack data structure.

Link to this section Summary

Functions

Get the index of given target in the stack

Get the sum of the elements in stack from index a to index b

Judge if the stack is empty or not

Return a new Stack with empty elements

Get the top element of given stack

Pop the top element of given stack

Pop the top k elements of given stack

Push an element into given stack

Get the size of given stack

Link to this section Functions

Link to this function find_index(stack, target)

Get the index of given target in the stack.

GIVEN : a stack, and the target to find

RETURNS : the index of the first target(0 index-based)

Examples

iex> Stack.find_index(%Stack{elements: [2, 1, 4, 7, 8]}, 4)
2
Link to this function getSum(stack, a, b)

Get the sum of the elements in stack from index a to index b.

GIVEN : a stack and a range start a, a range end b

RETURNS : the sum of elements between a and b

Examples

iex> Stack.getSum(%Stack{elements: [2, 1, 4, 7, 8]}, 1,3)
12
Link to this function isEmpty?(stack)

Judge if the stack is empty or not.

GIVEN : a stack

RETURNS : true iff the stack is empty

Examples

iex> Stack.isEmpty?(%Stack{elements: [2, 1, 4, 7, 8]})
false

iex> Stack.isEmpty?(%Stack{elements: []})
true

Return a new Stack with empty elements

Examples

iex> Stack.new
%Stack{elements: []}

Get the top element of given stack.

GIVEN : a stack

RETURNS : the top element of the stack

Examples

iex> Stack.peek(%Stack{elements: [2, 1, 4, 7, 8]})
2

Pop the top element of given stack.

GIVEN : a stack

RETURNS : the stack with the original top one removed

Examples

iex> Stack.pop(%Stack{elements: [2, 1]})
%Stack{elements: [1]}

iex> Stack.pop(%Stack{elements: []})
** (RuntimeError) Stack is empty!
Link to this function popFirstK(stack, k)

Pop the top k elements of given stack.

GIVEN : a stack and a number k

RETURNS : the stack with the original top k elements removed

Examples

iex> Stack.popFirstK(%Stack{elements: [2, 1, 4, 7, 8]}, 3)
%Stack{elements: [7, 8]}
Link to this function push(stack, element)

Push an element into given stack.

Given : a stack, and an element

RETURNS : push the element in the stack’s elements

Examples

iex> Stack.push(Stack.new, 2)
%Stack{elements: [2]}

Get the size of given stack.

GIVEN : a stack

RETURNS : the size of the stack

Examples

iex> Stack.size(%Stack{elements: [2, 1, 4, 7, 8]})
5