def InsertAtBegin(self, data): new_node = Node(data) if self.start is None: self.start = new_node return else: new_node.next = self.start self.start.prev = new_node self.start = new_node
def InserAtEnd(self, data): new_node = Node(data) if self.start is None: self.start = new_node return else: temp = self.start while (temp.next): temp = temp.next temp.next = new_node new_node.prev = temp
def InsertAtGivenPosition(self, pos, data): new_node = Node(data) if (pos == 0): self.InsertAtBegin(data) else: current = self.start previous = None current_position = 0 while (current_position < pos): previous = current current = current.next current_position += 1 new_node.next = current previous.next = new_node new_node.prev = previous current.prev = new_node
def append(self, data, *args): if isinstance(data, Node): data = data.data self.size += 1 node = Node(data, previous=self.tail) if self.tail: self.tail.next = node else: self.head = node self.tail = node for arg in args: self.append(arg)
def add(self, data, *args): if isinstance(data, Node): data = data.data self.size += 1 node = Node(data, self.head) if self.head: self.head.previous = node else: self.tail = node self.head = node for arg in args: self.add(arg)
def insert(self, data, index): if isinstance(data, Node): data = data.data if index == 0: self.add(data) return if index == self.size: self.append(data) return node = self.get(index) prev = node.previous new = Node(data, node, prev) prev.next, node.previous = new, new self.size += 1
def createNode(self, val): node = Node() node.setVal(val) node.setPrev(None) node.setNext(None) return node