def insert_first(self, value): node_value = DoublyNode(value) node_value.next = self.head.next node_value.previous = self.head node_value.next.previous = node_value self.head.next = node_value self.__length += 1
def insert_last(self, value): node_value = DoublyNode(value) node_value.previous = self.tail.previous node_value.next = self.tail node_value.previous.next = node_value self.tail.previous = node_value self.__length += 1
def insert_at(self, value, position): if position < 1: return self.insert_first(value) if position >= len(self): return self.insert_last(value) new_node = DoublyNode(value) position_node = self.__get_node_at(position) new_node.next = position_node new_node.previous = position_node.previous new_node.previous.next = new_node position_node.previous = new_node self.__length += 1
def __init__(self): self.head = DoublyNode("HEAD") self.tail = DoublyNode("TAIL") self.head.next = self.tail self.tail.previous = self.head self.__length = 0
def get_element_at(self, position): value_returned = None value_to_be_returned = self.__get_node_at(position) if value_to_be_returned: value_returned = DoublyNode(value_to_be_returned.value) return value_returned