def add_at_index(self, index, data): '''Add node at index''' node = Node(data) if self.head is None and index != 0: return False if index == 0: node.next = self.head self.head = node return prev = None current = self.head count = 0 while current.next and count != index: prev = current current = current.next count += 1 if count != index: return False prev.next = node node.next = current self.size += 1
def add(self, data): '''Add node''' node = Node(data) if self.head is None: self.head = node else: self.head.prev = node node.next = self.head self.head = node
def add(self, data): '''Add node to list (at head)''' node = Node(data) if self.head is None: self.head = node self.size += 1 return else: node.next = self.head self.head = node self.size += 1
def add_at_tail(self, data): '''Add node at tail''' node = Node(data) if self.head is None: self.head = node else: last = None current = self.head while current is not None: last = current current = current.next last.next = node node.prev = last
def add_after(self, node_data, data): ''' Add node after another node with passed data''' node = Node(data) if self.head is None: return False current = self.head while current.data != node_data and current.next is not None: current = current.next if current.data != node_data: return False node.next = current.next current.next = node self.size += 1
def add_at_tail(self, data): node = Node(data) if self.head is None: self.head = node self.head.next = self.head self.size += 1 elif self.head == self.head.next: node.next = self.head self.head.next = node self.size += 1 else: current = self.head while current.next is not self.head: current = current.next current.next = node node.next = self.head self.size += 1
def add_after(self, data, node_data): node = Node(data) if self.head is None: return False elif self.head == self.head.next and self.head.data == node_data: node.next = self.head self.head.next = node self.size += 1 else: current = self.head while current.data != node_data and current.next is not self.head: current = current.next if current.data != node_data: return False node.next = current.next current.next = node self.size += 1
def add_at_index(self, data, index): node = Node(data) if self.head is None and index != 0: return False if index == 0: if self.head is None: self.head = node self.head.next = self.head else: current = self.head while current.next is not self.head: current = current.next node.next = self.head self.head = node current.next = self.head else: if self.head is None: return False prev = None current = self.head count = 0 while current.next is not self.head and count != index: prev = current current = current.next count += 1 if count != index: return False node.next = current prev.next = node
def add_at_end(self, data): '''Add node to end of list (tail)''' node = Node(data) if self.head is None: self.head = node self.size += 1 return current = self.head while current.next is not None: current = current.next current.next = node self.size += 1
def insertNode(self, value, parent, tag): node = Node(parent, value, tag) if parent is not None: self.children.append(node) self.numChildren = self.numChildren + 1 else: self.root = node if tag == '1': self.depth1.append(node) elif tag == '2': self.depth2.append(node) elif tag == '3': self.depth3.append(node) elif tag == '4': self.depth4.append(node) return node