def addBefore(self, elementBefore, element): print("#Initially keeping elementFound = False") elementFound = False print("Save the head position") tempHead = self.head while(self.head.next != None): if self.head.next.data == elementBefore : elementFound = True print("#Getting current element position") getCurrElement = self.head print("#Getting next head position") getnextElement = self.head.next print("#Creating node object") nodeObject = Node(element) self.head.next = nodeObject nodeObject.next = getnextElement self.head = nodeObject.next self.head = self.head.next if elementFound == False: print("Element Not Found") self.head = tempHead
def reverse(self): print("#--- Begin Reversal Of Linked List ---") getTemp = self.head if self.head != None: finalList = Node(self.head.data) self.head = self.head.next while (self.head != None): tempListNode = Node(self.head.data) tempListNode.next = finalList finalList = tempListNode self.head = self.head.next self.head = finalList print("#--- End Reversal Of Linked List ---")
def reverse(self): print("#--- Begin Reversal Of Linked List ---") getTemp = self.head if self.head != None: finalList = Node(self.head.data) self.head = self.head.next while(self.head != None): tempListNode = Node(self.head.data) tempListNode.next = finalList finalList = tempListNode self.head = self.head.next self.head = finalList print("#--- End Reversal Of Linked List ---")
def add(self, element): print("#--- Begin insertion at begining") print("#Creating node object") nodeObject = Node(element) print("#Check if first element or not") if self.head == None: print("#It's the first element") self.head = self.tail = nodeObject print("#Element Added") else: print("#It's not the first element, adding the element") self.head.previous = nodeObject nodeObject.next = self.head self.head = nodeObject
def add(self, data): print("#--BEGIN INSERTING DATA AT BEGINING--") print("#Creating node object") nodeObject = Node(data) print("#Checking head position") tempHead = self.head if self.head == None: print("#Head is null, so adding first element") self.head = self.tail = nodeObject else: print("#Head is not null, adding the nodeobject in the starting") nodeObject.next = self.head self.head = nodeObject
def addElementAfterAnElement(self, afterElement, element): dataFound = False; getHead = self.head print("#Finding element") while(self.head != None): if self.head.data == afterElement: dataFound = True print("#Element FOUND !") print("#Creating node object") nodeObject = Node(element); print("#Adding Element") tempSelfHeadNext = self.head.next self.head.next = nodeObject nodeObject.next = tempSelfHeadNext print("#--- ELEMENT ADDED ---") self.head = self.head.next if dataFound == False: print("element not found") self.head = getHead