def __setitem__(self, index, newItem): current = self.head counter = 0 while current is not None: if counter == index: current = Node(newItem, current.getPrev(), current.getNext()) counter += 1 current = current.getNext()
def append(self, elem): if self.__len__() < 2: if self.__len__() == 1: tmp = Node(elem, self.head, None) self.tail = tmp self.head.setNext(tmp) else: self.head = Node(elem) else: tmp = Node(elem, self.tail, None) self.tail.setNext(tmp) self.tail = tmp
def push(self, elem): tmp = Node(elem, None, self.head) if self.head is None: self.tail = tmp if self.head is not None and self.head.getNext() is None: self.tail = self.head self.head = tmp
def push(self, elem): tmp = Node(elem, None, self.head) if self.__len__() < 2: if self.__len__() == 0: self.head = tmp else: self.tail = self.head self.head = tmp self.tail.setPrev(self.head) else: self.head.setPrev(tmp) self.head = tmp
def insert(self, elem, index): if index == 0: self.push(elem) else: counter = 0 current = self.head while counter < index - 1: current = current.getNext() counter += 1 tmp = Node(elem, current, current.getNext()) current.setNext(tmp) current.getNext().setPrev(tmp)
def insert(self, elem, index): if index == 0: # Base Case self.push(elem) else: counter = 0 current = self.head while counter < index - 1: current = current.getNext() counter += 1 tmp = Node(elem, None, current.getNext()) current.setNext(tmp) if current is self.tail: self.tail = tmp print("Value", elem, "has been inserted into the list at position", index)
class DoubleLinkedList(List): def __init__(self, head=None, tail=None): super(self.__class__, self).__init__(head, tail) def __setitem__(self, index, newItem): current = self.head counter = 0 while current is not None: if counter == index: current = Node(newItem, current.getPrev(), current.getNext()) counter += 1 current = current.getNext() def insert(self, elem, index): if index == 0: self.push(elem) else: counter = 0 current = self.head while counter < index - 1: current = current.getNext() counter += 1 tmp = Node(elem, current, current.getNext()) current.setNext(tmp) current.getNext().setPrev(tmp) def append(self, elem): if self.__len__() < 2: if self.__len__() == 1: tmp = Node(elem, self.head, None) self.tail = tmp self.head.setNext(tmp) else: self.head = Node(elem) else: tmp = Node(elem, self.tail, None) self.tail.setNext(tmp) self.tail = tmp def push(self, elem): tmp = Node(elem, None, self.head) if self.__len__() < 2: if self.__len__() == 0: self.head = tmp else: self.tail = self.head self.head = tmp self.tail.setPrev(self.head) else: self.head.setPrev(tmp) self.head = tmp def reverse(self): start = self.head end = self.tail for i in range(0, self.__len__() // 2): tmp = start.getElement() start.setPrev(end.getPrev()) start.setNext(end.getNext()) start.setElement(end.getElement()) end.setPrev(start.getPrev()) end.setNext(start.getNext()) end.setElement(tmp) start = start.getPrev() end = end.getNext()
def append(self, elem): tmp = Node(elem) self.tail.setNext(tmp) self.tail = tmp