def testCaseForIntersected(): l1 = LinkedList() currentNode = l1.head for i in range(5): n = Node(i) if currentNode is None: l1.head = n currentNode = l1.head else: currentNode.next = n currentNode = currentNode.next l1.printList() l2 = LinkedList() currentNode = l1.head counter = 0 while counter < 3: currentNode = currentNode.next counter +=1 l2.head = currentNode l2.printList() # call the functions result = isIntersectBtw(l1,l2) print("Intersect Result: " + str(result))
def createLinkedListWrt(hashTable): newList = LinkedList() for key in hashTable.keys(): newList.insertTail(key) return newList
def _createLinkedList(self, dataList): linkedList = LinkedList() for i in dataList: linkedList.insertTail(i) return linkedList
def getreversedLinkedList(linkedList): reversedLinkedList = LinkedList() node = linkedList.head while node is not None: reversedLinkedList.insertHead(node.data) node = node.next return reversedLinkedList
def convertToLinkedList(number): linkedList = LinkedList() numberLength = len(str(number)) division = 1 for i in range(numberLength - 1): division *= 10 for i in range(numberLength): data = int(number / division) linkedList.insertTail(data) number %= division division /= 10 return linkedList
def testCaseForLoopDetection(): #circular linkedlist l1 = LinkedList() currentNode = l1.head for i in range(5): n = Node(i) if currentNode is None: l1.head = n currentNode = l1.head else: currentNode.next = n currentNode = currentNode.next currentNode.next = l1.head.next.next # l1.printList() # call the functions result = getBeginningNodeForLoop(l1) if result: print("Started Node for loop: " + str(result.data))
division = 1 for i in range(numberLength - 1): division *= 10 for i in range(numberLength): data = int(number / division) linkedList.insertTail(data) number %= division division /= 10 return linkedList if __name__ == '__main__': l1 = LinkedList() l1.insertTail(1) l1.insertTail(2) l1.insertTail(3) l1.printList() l2 = LinkedList() l2.insertTail(3) l2.insertTail(2) l2.insertTail(1) l2.printList() l1Number = convertToInteger(l1) l2Number = convertToInteger(l2)
def createLinkedListWrt(hashTable): newList = LinkedList() for key in hashTable.keys(): newList.insertTail(key) return newList def getRemovedDuplicatesOnLinkedList(linkedList): hashTable = convertLinkedListToHashTable(linkedList) newList = createLinkedListWrt(hashTable) return newList if __name__ == '__main__': #prepare the List l1 = LinkedList() l1.insertTail(5) l1.insertTail(1) l1.insertTail(2) l1.insertTail(3) l1.insertTail(4) l1.insertTail(3) l1.insertTail(5) l1.printList() l2 = getRemovedDuplicatesOnLinkedList(l1) l2.printList()
def testCaseForNotIntersected(): l1 = LinkedList() l1.insertTail(1) l1.insertTail(2) l1.insertTail(3) l2 = LinkedList() l2.insertTail(1) l2.insertTail(2) l2.insertTail(3) # call the functions result = isIntersectBtw(l1, l2) print("Intersect Result: " + str(result))
reversedLinkedList = getreversedLinkedList(linkedList) node = linkedList.head reversedNode = reversedLinkedList.head while node is not None: if node.data is not reversedNode.data: return False node = node.next reversedNode = reversedNode.next return True if __name__ == '__main__': l1 = LinkedList() l1.insertTail('m') l1.insertTail('a') l1.insertTail('d') l1.insertTail('a') l1.insertTail('m') l1.printList() result = isPalindrome(l1) print(result) l2 = LinkedList() l2.insertTail('a') l2.insertTail('d') l2.insertTail('a')
def __init__(self): self.dogList = LinkedList() self.catList = LinkedList() self.date = 0
class AnimalShelter: def __init__(self): self.dogList = LinkedList() self.catList = LinkedList() self.date = 0 def enqueue(self, animalName, animalType): self.date += 1 if animalType == AnimalType.Cat: node = AnimalNode(animalName, self.date) self.catList.insertTail(node) elif animalType == AnimalType.Dog: node = AnimalNode(animalName, self.date) self.dogList.insertTail(node) else: self.date -= 1 def findOldestAnimal(self, linkedList): oldestAnimalNode = linkedList.head date = oldestAnimalNode.data.date traverseNode = linkedList.head while traverseNode.next is not None: if date > traverseNode.next.data.date: oldestAnimalNode = traverseNode.next traverseNode = traverseNode.next return oldestAnimalNode def dequeuDog(self): if self.dogList.isEmpty(): print("Error: the list empty") return else: oldestNode = self.findOldestAnimal(self.dogList) self.dogList.removeNode(oldestNode.data) return oldestNode def dequeuCat(self): if self.catList.isEmpty(): print("Error: the list empty") return else: oldestNode = self.findOldestAnimal(self.catList) self.catList.removeNode(oldestNode.data) return oldestNode def dequeueAny(self): oldestCatNode = None oldestDogNode = None if self.catList.isEmpty(): print("Warning: cat the list empty") else: oldestCatNode = self.findOldestAnimal(self.catList) if self.dogList.isEmpty(): print("Warning: dog list empty") else: oldestDogNode = self.findOldestAnimal(self.dogList) if oldestDogNode.data.date > oldestCatNode.data.date: self.catList.removeNode(oldestCatNode.data) return oldestCatNode else: self.dogList.removeNode(oldestDogNode.data) return oldestDogNode def printAll(self): print("Printing Dog") self.dogList.printList() print("Printing Cat") self.catList.printList()
from Utils.LinkedList import LinkedList if __name__ == '__main__': l1 = LinkedList() l1.insertTail(1) l1.insertTail(2) l1.insertTail(3) l1.insertTail(4) l1.printList() l1.removeNode(3) l1.printList()