dsa.stack module — ucxdsa documentation

# dsa.stack module

Module containing stack classes.

class dsa.stack.DynamicStack(capacity=10)

  Bases: `Stack`

  A dynamic stack implementation.

  check_capacity()

    Check the capacity of the stack. If count >= capacity, grow the array If count <= 1/4 of capacity, shrink the array

  grow()

    Helper method to double the capacity of the current array.

  pop()

    Return an element from the stack. Automatically shrinks array if capacity is 4x the count.

    Returns:

      The top element in the stack.

    Raises:

      Exception – When the stack is empty.

  push(element)

    Push an element into the stack. Automatically grows array if capacity needs to increase.

    Parameters:

      element – The element to push.

    Returns:

      None

  shrink()

    Helper method to halve the capacity of the current array.

class dsa.stack.Stack(capacity=10)

  Bases: `object`

  A static stack implementation.

  capacity()

    Return the capacity of the stack.

  count

    number of elements in stack

  classmethod from_list(alist)

    Set the contents of a stack from a list. Raise Exception when trying to push more elements than the capacity.

    Parameters:

      alist (`list`) – The list with contents to set as an array.

  is_empty()

    Return a Boolean on whether the stack is empty or not.

  peek()

    Return the element from the top of the stack. Raise Exception if stack is empty.

    Returns:

      The top element in the stack.

    Raises:

      Exception – When the stack is empty.

  pop()

    Pop an element from the stack. Raise Exception when there are no elements to pop.

    Returns:

      The top element in the stack.

    Raises:

      Exception – When the stack is empty.

  push(element)

    Push an element into the stack. Raise Exception when trying to push more elements than the capacity.

    Parameters:

      element – The element to push.

    Raises:

      Exception – When the capacity is reached.

  to_list()

    Return the contents of the stack as an array.

  top()

    Return the top index of the stack.