def append(self, x): """Add an item to the end of the list.""" _node = Node(x, None, None) if self._head is not None: # if the head is not None the previously of new node become the tail and the # next one None _node._prev = self._tail # previously of new node linked to tail _node._next = None # the next of new new node linked to None self._tail._next = _node # the tail's next linked to new node self._tail = _node # new node become the tail else: # if the head is None there aren't node, so the new node is tail and the head self._head = self._tail = _node
def insertbeforenode(self, data, value_search): new_node = Node(data, None, None) curr_node = self._head while curr_node: if curr_node._value == value_search._value: new_node._prev = curr_node._prev new_node._next = curr_node curr_node._prev = new_node new_node._prev._next = new_node # <-- break # find node and quit loop else: curr_node = curr_node._next
def insert(self, i, x): new_node = Node(x, None, None) curr_node = self._head if i > self.__len__(): return 'index out of range' # list is empty if self.isEmpty(): self._head = new_node self._head._prev = self._head else: if i == 0: new_node._next = self._head self._head._prev = new_node self._head = new_node if self.__len__() == 0: self._tail = new_node return elif i == self.__len__(): self.append(new_node._value) return else: self.insertbeforenode(x, self.__getitem__(i - 1))