def append(self, new_data): new_node = Node(data = new_data) new_node.next = None # at the last last = self.tail # if list is empty then this is the first node if last is None: new_node.prev = None self.head = new_node return last.next = new_node new_node.prev = last
def push(self, new_data): new_node = Node(data = new_data) new_node.prev = None # at the beginning new_node.next = self.head if self.head is not None: self.head.prev = new_node self.head = new_node
def insert_before(self, next_node, new_data): if next_node is None: print("Error: given next_node is None") return new_node = Node(data = new_data) new_node.next = next_node new_node.prev = next_node.prev if new_node.prev is not None: # if new_node.prev (next_node.prev) is None it means we are at the beginning of the list new_node.prev.next = new_node next_node.prev = new_node
def insert_after(self, prev_node, new_data): if prev_node is None: print("Error: given prev_node is None") return new_node = Node(data = new_data) new_node.prev = prev_node new_node.next = prev_node.next if new_node.next is not None: # if new_node.next (prev_node.next) is None it means we are at the end of the list new_node.next.prev = new_node prev_node.next = new_node