def test3(): #scenario: End node is the intersection LLend1 = LinkedList() LLend2 = LinkedList() endNode = Node("end") for num in range(19): LLend1.push(num) LLend2.push(num) print(num) LLend1.pushNode(endNode) LLend2.pushNode(endNode) result = findIntersection(LLend1, LLend2) #count = 435 # length of both lists = 19 # This is the worst case, and the worst case of checking for the end node, does not stop with the check. print(result)
def test2(): #worst case: Loop node is at the end, pointing at the first node. LLisLoop2 = LinkedList() loopNode2 = Node("LOOP2") for i in range(20): if i == 19: LLisLoop2.pushNode(loopNode2) else: LLisLoop2.push(i) #set the loop begin = LLisLoop2.begin loopNode2.next = begin result = findLoop(LLisLoop2) #count = 18 print(result)
#chapter 2 problem 7 from ch2_SetUp import Node, LinkedList LL1 = LinkedList() LL2 = LinkedList() LL3 = LinkedList() for char in "LIST": LL3.push(char) for char2 in "0101linked": LL2.push(char2) for char3 in "LINK2D": LL1.push(char3) nextNode = LL3.begin LL1.pushNode(nextNode) LL2.pushNode(nextNode) #LL1.printList() #print('-' * 10) #LL2.printList() #worst case is no intersection LL4 = LinkedList() LL5 = LinkedList() for char4 in 'notintersecting': LL4.push(char4) LL5.push(char4)
#chapter 2 problem 8 from ch2_SetUp import Node, LinkedList # CAUTION: # using printList() *while loop* on a linkedlist with a loop, will cause an infinite loop of printing. LLisLoop = LinkedList() loopNode = Node("LOOP") for i in range(20): if i == 10: LLisLoop.pushNode(loopNode) else: LLisLoop.push(i) #set the loop LLisLoop.end.next = loopNode def findLoop(linkedlist): node1 = linkedlist.begin count = 0 if node1 == None: print("This linked list is empty") return None runner = linkedlist.begin.next if runner: while node1 and runner: if node1 == runner: print("loop node found.")