def insert_at_index(self, index, data): if index == 1: new_node = Node(data) new_node.next = self.head self.head = new_node if index > 1: i = 1 while i < index - 1 and self.head: i = i + 1 self.head = self.head.next if self.head is None: print("Index out of bound") return new_node = Node(data) new_node.next = self.head.next self.head.next = new_node
def insert_before_item(self, x, data): if self.head is None: print("List has no element") return if self.head.data == x: new_node = Node(data) new_node.next = self.head self.head = new_node while self.head.next: if self.head.next.data == x: break self.head = self.head.next if self.head.next is None: print("Item doesn't exist") return new_node = Node(data) new_node.next = self.head.next self.head.next = new_node
def prepend(self, value): """ Adds the value to the start of the list :param value: the value to be added """ node = Node(value) node.next = self.head self.head = node
def insert_after_item(self, x, data): new_node = Node(data) while self.head.data != x and self.head: self.head = self.head.next if self.head is None: print("Item doesn't exist") return after_node = self.head.next self.head.next = new_node new_node.next = after_node
def prepend(self, value): """ Adds the value to the start of the list :param value: the value to be added """ new_node = Node(value) current_node = self.head new_node.next = current_node self.head = new_node
def prepend(self, value): node = Node(value) node.next = self.head self.head.previous = node self.head = node
def insert_at_start(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node