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 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 add(self,element): print("#Begin Insertion of element using doubly linkedlist") print("#Creating node object") nodeObject = Node(element) print("#Check whether it's the first element or not") if self.head == None: print("#It's the first element") self.head = self.tail = nodeObject print("#ELEMENT ADDED SUCCESSFULLY") else: print("#Its not the first element") print("#Adding element") print("#Get the tail and save it in temp var") tempTail = self.tail print("#Now update the tail element with previous and next") nodeObject.previous = self.tail self.tail.next = nodeObject self.tail = nodeObject print("#ELEMENT ADDED SUCCESSFULLY")
def add(self, element): print("#Begin Insertion of element using doubly linkedlist") print("#Creating node object") nodeObject = Node(element) print("#Check whether it's the first element or not") if self.head == None: print("#It's the first element") self.head = self.tail = nodeObject print("#ELEMENT ADDED SUCCESSFULLY") else: print("#Its not the first element") print("#Adding element") print("#Get the tail and save it in temp var") tempTail = self.tail print("#Now update the tail element with previous and next") nodeObject.previous = self.tail self.tail.next = nodeObject self.tail = nodeObject print("#ELEMENT ADDED SUCCESSFULLY")
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
def add(self, data): print("creating node object") nodeObject = Node(data) print("#Checking head") if self.head == None: print("#Head is none, adding first element") self.head = self.tail = nodeObject else: print("#Head is not none, adding an element to the end of list") self.tail.next = nodeObject self.tail = nodeObject print("#----- ELEMENT SUCCESSFULLY ADDED ELEMENT : " + data + " -----")
def add(self, data): print("#create node object") nodeObject = Node(data); print("#check head and tail") if self.head == None: print("#Head is none, adding first element") self.head = self.tail = nodeObject else: print("#Head is not null, appending an element") self.tail.next = nodeObject; self.tail = nodeObject; print("#--- Element added : " + data)
def add(self, data): print("#Adding data") print("#Checking whether head is null or not") print("Creating node object") nodeObject = Node(data) if self.head == None: print("Head is null, so adding first element") self.head = self.tail = nodeObject else: print("Head is not null, appending element") self.tail.next = nodeObject self.tail = nodeObject
def add(self, data): print("#Begin addition of element") print("#Creating node object") nodeObject = Node(data) print("#Check whether head is null or not") if (self.head == None): print("#Head is null, adding first element") self.head = self.tail = nodeObject else: print("#Head is not null, appending element") self.tail.next = nodeObject self.tail = nodeObject print("#Finish addition of element")