def setUp(self): self.example_value_1 = 'example value 1' self.example_value_2 = 'example value 2' self.example_value_3 = 'example value 3' self.example_value_list = [1, '2', 3.0, 4] self.example_node_1 = Node(value=self.example_value_1) self.example_node_2 = Node(value=self.example_value_2) self.example_node_3 = Node(value=self.example_value_3) self.example_linked_list = LinkedList(self.example_value_1) self.example_stringified_list = 'example value 2\nexample value 1\n' self.example_stringified_list_2 = 'example value 1\nexample value 2\n' self.example_stringified_list_3 = '1\n2\n3.0\n4\n' self.example_empty_list = LinkedList() self.example_empty_list._head_node = None pass
def insert_beginning(self, new_value): """ Inserts the new node with new value to the beginning of the linked list @param new_value: value of the inserting new node """ new_node = Node(value=new_value, link_node=self._head_node) self._head_node = new_node
def _construct_from_list(self, ref_list): """ **Reconstructs** the linked list from a list of values @param ref_list: reference list for constructing the linked list """ self._head_node = Node(ref_list[0]) [self.add_value_to_the_end(item) for item in ref_list[1:]]
def __init__(self, value=None, ref_list='', by_list=False): """ Can be constructed in two ways, when by_list is true, it constructs with ref_list otherwise it constructs with value @param value: value of head node @param ref_list: reference list for reconstructing the linked list @param by_list: <bool> whether we use reference list to construct """ self._head_node = Node(value=value) if by_list is True: self._construct_from_list(ref_list)
def add_value_to_head(self, value): """ Add the value to the head of linked list @param value, value to te be added """ new_node = Node(value) if not self._head_node: self._head_node = new_node return new_node.set_link_node(self._head_node) self._head_node = new_node
def add_value_to_the_end(self, value): """ Add the value to the end of linked list @param value: value to be added """ new_node = Node(value) if not self._head_node: self._head_node = new_node return current = self._head_node while current.get_link_node(): current = current.get_link_node() current.set_link_node(new_node)
def add_value(self, position, value): """ Add note to the specified position, if position is larger than the length of linked list, place the new node to the end @param position: <int> the position where the node should be placed, order is from 0 @param value: <value> the value to be added """ if position == 0: self.add_value_to_head(value) return new_node = Node(value) count = 1 current = self._head_node while current is not None: if count == position: new_node.set_link_node(current.get_link_node()) current.set_link_node(new_node) return current = current.get_link_node() count += 1 self.add_value_to_the_end(value)
def setUp(self): self.example_node = 'node_1' self.example_value = 'example_value' self.example_link_node = 'example_link_node' self.example_node_with_properties = Node( value=self.example_value, link_node=self.example_link_node)
def _create_node(): """ Create a Node instance @return: <Node> Node instance """ return Node('example value')