def test_find(self): print('Test: find on an empty list') linked_list = LinkedList(None) node = linked_list.find('a') assert_equal(node, None) print('Test: find a None') head = Node(10) linked_list = LinkedList(head) node = linked_list.find(None) assert_equal(node, None) print('Test: find general case with matches') head = Node(10) linked_list = LinkedList(head) linked_list.insert_to_front('a') linked_list.insert_to_front('bc') node = linked_list.find('a') assert_equal(str(node), 'a') print('Test: find general case with no matches') node = linked_list.find('aaa') assert_equal(node, None) print('Success: test_find\n')
def partition(headNode, x): less_list = None greater_list = None if headNode == None: return 'cannot partition empty linkedlist' while headNode != None: current_data = headNode.data if current_data < x: if less_list == None: less_list = Node(current_data) else: less_list.addToTail(current_data) else: if greater_list == None: greater_list = Node(current_data) else: greater_list.addToTail(current_data) headNode = headNode.nextNode if less_list == None: return greater_list elif greater_list == None: return less_list else: less_list.addNodeToTail(greater_list) result = less_list return result
def partition(chain: Node, target: int) -> LinkedList: beforeStart = Node() beforeEnd = Node() afterStart = Node() afterEnd = Node() while (chain): nex = chain.next chain.next = None # Used so that we dont accidently create a circular linked list if chain.val < target: if beforeStart.val == None: # Kind of like a bool beforeStart = chain beforeEnd = beforeStart else: beforeEnd.next = chain beforeEnd = chain else: if afterStart.val == None: afterStart = chain afterEnd = afterStart else: afterEnd.next = chain afterEnd = chain chain = nex if not beforeStart: return afterStart beforeEnd.next = afterStart return beforeStart
def reverse_linkedList(list_head): rev_list = Node(list_head.data) list_head = list_head.nextNode() while list_head != None: rev_list.addNodeToFront(list_head.data) list_head = list_head.nextNode() return rev_list
def reverse_linkedList(list_head): rev_list = Node(list_head.data) list_head = list_head.nextNode() while list_head != None: rev_list.addNodeToFront(list_head.data) list_head = list_head.nextNode() return rev_list
def push(self, item): n = Node(item) if self.top == None: self.top = n else: n.next = self.top self.top = n
def test_delete(self): print('Test: delete on an empty list') linked_list = LinkedList(None) linked_list.delete('a') assert_equal(linked_list.get_all_data(), []) print('Test: delete a None') head = Node(10) linked_list = LinkedList(head) linked_list.delete(None) assert_equal(linked_list.get_all_data(), [10]) print('Test: delete general case with matches') head = Node(10) linked_list = LinkedList(head) linked_list.insert_to_front('a') linked_list.insert_to_front('bc') linked_list.delete('a') assert_equal(linked_list.get_all_data(), ['bc', 10]) print('Test: delete general case with no matches') linked_list.delete('aa') assert_equal(linked_list.get_all_data(), ['bc', 10]) print('Success: test_delete\n')
def testRemove(self): print('Test: remove element from an empty linked list') linkedList = LinkedList(None) linkedList.remove(2) self.assertEqual(linkedList.getData(), []) print('Test: remove a None') head = Node(1) linkedList = LinkedList(head) linkedList.remove(None) self.assertEqual(linkedList.getData(), [1]) print('Test: remove element from a non-empty linked list') head = Node(1) linkedList = LinkedList(head) linkedList.insertEnd(2) linkedList.insertEnd(3) linkedList.insertStart(0) linkedList.remove(0) linkedList.remove2(2) self.assertEqual(linkedList.getData(), [1, 3]) linkedList.remove('abc') self.assertEqual(linkedList.getData(), [1, 3]) print('Success: testRemove')
def test_LinkedList(): l1 = LinkedList() l1.head = Node('Mon') e2 = Node('Tue') e3 = Node('Wed') l1.head.next = e2 # link first Node to second Node e2.next = e3 # link secodn node to third Node return l1
def test_find_in_list_with_many_nodes(self): s_list_example = LinkedList() list_elements = [34, 45, 1, 2, 34, 6, 8, 12, 34] for i in list_elements: s_list_example.add_in_tail(Node(i)) self.assertEqual(s_list_example.find_all(Node(555)), []) self.assertEqual(len(s_list_example.find_all(34)), 3) self.assertEqual(len(s_list_example.find_all(45)), 1)
def setUp(self): # Set up Nodes e1 = Node(1) e2 = Node(2) e3 = Node(3) # Start setting up a LinkedList self.ll = LinkedList(e1) self.ll.append(e2) self.ll.append(e3)
def insert(self, elem): node = Node(elem) node.next = None if self.head == None: self.head = node self.tail = node else: self.tail.next = node self.tail = node self.length += 1
def correctInsertion(self, data): newNode = Node(data) current_node = self.root.next previous_node = self.root while (current_node.next != None and current_node.data > data): previous_node = current_node current_node = current_node.next previous_node.next = newNode newNode.next = current_node self.length += 1
def insert(self, elem): node = Node(elem) node.next = None if self.head == None: self.head = node self.tail = node else: self.tail.next = node self.tail = node self.length += 1
def add2Sum(l1, l2): print('') print('inside function...') # Declare pointers for traversal. Added for clarity. p1 = l1.head p2 = l2.head # Declare current carry over. carry = 0 # Declare cur variable to help traverse and add nodes to new list. # Declare head variable to be the head of the list. head = cur = Node(0) # Iteration condition. while p1 or p2 or carry: #print(p1.val, p2.val, carry) # Determine current value and carry over. sum = carry sum += 0 if p1 is None else p1.val sum += 0 if p2 is None else p2.val if sum >= 10: sum -= 10 carry = 1 else: carry = 0 print(sum, carry) # Add current value as it will always atleast be '1'. cur.next = Node(sum) cur = cur.next # Add base cases for iterating linked lists. if p1 is None and p2 is None: break elif p1 is None: p2 = p2.next elif p2 is None: p1 = p1.next else: p1 = p1.next p2 = p2.next print('exiting...') print('') temp = head.next while (temp is not None): print(temp.val) temp = temp.next
def removeDups(self, head): curr = head dummy = Node(0) while curr is not None: if curr.val not in self.cache: self.cache[curr.val] = True dummy.next = curr dummy = dummy.next curr = curr.next return head
def reverseLinkedList(linkedList): result = None while linkedList != None: data = linkedList.data if result == None: result = Node(data) else: node = Node(data) node.nextNode = result result = node linkedList = linkedList.nextNode return result
def test_find_all_not_empty(self): list2 = LinkedList() n1 = Node(55) n2 = Node(12) n3 = Node(55) list2.add_in_tail(n1) list2.add_in_tail(n2) list2.add_in_tail(n3) self.assertEqual(list2.find_all(n3.value), [n1, n3]) self.assertEqual(list2.get_head(), n1) self.assertEqual(list2.get_tail(), n3)
def test_loop(): LL = LinkedList() node = Node(3) LL.head = Node(1) LL.head.next = Node(2) LL.head.next.next = node LL.head.next.next.next = Node(4) LL.head.next.next.next.next = Node(5) LL.head.next.next.next.next = node assert node == loopDetection(LL)
def reverseLinkedList(linkedList): result = None while linkedList != None: data = linkedList.data if result == None: result = Node(data) else: node = Node(data) node.nextNode = result result = node linkedList = linkedList.nextNode return result
def add(self, item): if not self.head: temp = Node(item) temp.setNext(self.head) self.head = temp else: current = self.head previous = None found = False while current != None: if current.getData() > item: found = True temp = Node(item) temp.setNext(current) if not previous: self.head = temp else: previous.setNext(temp) break else: previous = current current = current.getNext() if not found: self.addAtEnd(item) if not self.last: self.last = self.head
def sumReverseOrder(l1, l2): carry = 0 dummy = Node(0) ans = dummy while l1 and l2: r = (l1.val + l2.val + carry) % 10 dummy.next = Node(r) carry = (l1.val + l2.val + carry) / 10 dummy = dummy.next l1 = l1.next l2 = l2.next while l1: r = (l1.val + carry) % 10 dummy.next = Node(r) carry = (l1.val + carry) / 10 dummy = dummy.next l1 = l1.next while l2: r = (l2.val + carry) % 10 dummy.next = Node(r) carry = (l2.val + carry) / 10 dummy = dummy.next l2 = l2.next return ans.next
def partition(headNode, x): less_list = None greater_list = None if headNode == None: return 'cannot partition empty linkedlist' while headNode != None: current_data = headNode.data if current_data < x: if less_list == None: less_list = Node(current_data) else: less_list.addToTail(current_data) else: if greater_list == None: greater_list = Node(current_data) else: greater_list.addToTail(current_data) headNode = headNode.nextNode if less_list == None: return greater_list elif greater_list == None: return less_list else: less_list.addNodeToTail(greater_list) result = less_list return result
def test_mergeListsWithNonEqualLength(self): list1 = LinkedList() list2 = LinkedList() elements1 = [2, 3, 4, 5, 6, 7] elements2 = [5, 1, 2, 8, 11] for i in elements1: list1.add_in_tail(Node(i)) for i in elements2: list2.add_in_tail(Node(i)) self.assertEqual(mergeLists(list1, list2), [[], -1])
def test_insert_to_body(self): list3 = LinkedList() n1 = Node(55) n2 = Node(12) n3 = Node(55) n4 = Node(10) list3.add_in_tail(n1) list3.add_in_tail(n2) list3.add_in_tail(n3) list3.insert(n2, n4) self.assertEqual(list3.get_all(), [ n1.value, n2.value, n4.value, n3.value]) self.assertEqual(list3.get_head(), n1) self.assertEqual(list3.get_tail(), list3.find_all(n1.value)[1])
def setUp(self): # node creation # [10, 12, 14, 16, 18] self.nodes = [Node(i) for i in range(10,20,2)] # linked list creation self.singleList = SinglyLinkedList() self.singleList.listSize = 0
def initSession(self): """ load and initialise previous session :return: None """ if os.path.isfile("tracks.lib"): file = open("tracks.lib", "r") for i in file.readlines(): self.player.library.append(i) self.libraryBox.insert(END, Node(i, None).title) index = 0 for file in os.listdir(os.getcwd()): if file.endswith(".pList"): fileName = os.path.splitext(os.path.basename(file))[0] self.playListBox.insert(END, fileName) self.playListMenu.add_command(label=fileName, command=functools.partial( self.addToPlayList, parent=self.playListMenu, index=index + 1)) self.appendMenu.add_command(label=fileName, command=functools.partial( self.appendPlist, parent=self.appendMenu, index=index)) self.mergeMenu.add_command(label=fileName, command=functools.partial( self.mergePlist, parent=self.mergeMenu, index=index)) index += 1
def loadPlayList(self, event): """ load playList from file based on user selection :param event: object that called the function :return: None """ self.player.linkedListQueue = SingleList() self.queue.delete(0, END) self.player.pygame.mixer.music.stop() if self.playListBox.size() > 0: w = event.widget index = int(w.curselection()[0]) x = self.playListBox.get(index) self.curPlayList = x file = open(x + ".pList", 'r') for location in file.readlines(): node = Node(location, None) self.queue.insert(0, node.title) self.player.linkedListQueue.addNode(node) self.currPlayThread = threading.Thread(target=self.songCheck) self.currPlayThread.start()
def DetectCycle(lst): ptr1 = Node() ptr2 = Node() ptr1 = ptr2 = lst while ptr1 != None and ptr1.next != None and ptr2 != None and ptr2.next != None: ptr1 = ptr1.next ptr2 = ptr2.next.next if ptr1 == ptr2: ptr1 = lst #move ptr1 back to beginning while ptr1 != ptr2: ptr1 = ptr1.next ptr2 = ptr2.next #increment both by 1 each time return ptr1 #return node where they met return False
def test_diff_len_intersection(): LL = LinkedList() LL2 = LinkedList() node = Node(4) node2 = Node(5) LL.head = Node(1) LL.head.next = node LL.head.next.next = node2 LL2.head = Node(10) LL2.head.next = Node(11) LL2.head.next.next = node LL2.head.next.next.next = node2 assert node == intersection(LL, LL2)
def test_insert_head(self): list3 = LinkedList() n1 = Node(55) n2 = Node(12) n3 = Node(55) n4 = Node(10) n5 = Node(20) list3.add_in_tail(n1) list3.add_in_tail(n2) list3.add_in_tail(n3) list3.add_in_tail(n4) list3.insert(None, n5) self.assertEqual(list3.get_all(), [ n5.value, n1.value, n2.value, n3.value, n4.value]) self.assertEqual(list3.get_head(), n5) self.assertEqual(list3.get_tail(), n4)
def test_insert_in_tail(self): list3 = LinkedList().create_list([20, 55, 12, 10, 55]) n6 = Node(22) list3.insert(list3.get_tail(), n6) self.assertEqual(list3.get_all(), [20, 55, 12, 10, 55, 22]) self.assertEqual(list3.get_head(), list3.find(20)) self.assertEqual(list3.get_tail(), n6)
def test_insert_to_empty(self): list3 = LinkedList() n7 = Node(11) list3.insert(None, n7) self.assertEqual(list3.get_all(), [n7.value]) self.assertEqual(list3.get_head(), n7) self.assertEqual(list3.get_tail(), n7)
def sumLists(head1, head2): add_next = 0 value = head1.value + head2.value if value >= 10: value -= 10 add_next = 1 sumlist = Node(value) s = sumlist head1 = head1.next head2 = head2.next while head1 and head2: value = head1.value + head2.value + add_next add_next = 0 if value >= 10: value -= 10 add_next = 1 s.next = Node(value) s = s.next head1 = head1.next head2 = head2.next while head1: s.next = head1 if add_next: if s.next.value + 1 < 10: s.next.value += 1 add_next = 0 else: s.next.value -= 9 add_next = 1 head1 = head1.next s = s.next while head2: s.next = head2 if add_next: if s.next.value + 1 < 10: s.next.value += 1 add_next = 0 else: s.next.value -= 9 add_next = 1 head2 = head2.next s = s.next if add_next: s.next = Node(1) printLinkedList(sumlist)
def test_delet_from_list_with_one_node(self): s_list_example = LinkedList() s_list_example.add_in_tail(Node(2)) s_list_example.delete(2, all=True) self.assertEqual(s_list_example.len(), 0) self.assertEqual(s_list_example.head, None) self.assertEqual(s_list_example.tail, None)
def sumLinkedListReverse(first, second): if first == None and second == None: return None carry = 0 result = None while first != None or second != None or carry != 0: digit_sum = carry if first != None: digit_sum += first.data first = first.nextNode if second != None: digit_sum += second.data second = second.nextNode if result == None: result = Node(digit_sum%10) else: result.addToTail(digit_sum%10) carry = digit_sum / 10 return result
if ptr1 == ptr2: ptr1 = lst #move ptr1 back to beginning while ptr1 != ptr2: ptr1 = ptr1.next ptr2 = ptr2.next #increment both by 1 each time return ptr1 #return node where they met return False """ Tests """ n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) lst1 = n1 lst1.next = n2 n2.next = n3 n3.next = n4 assert DetectCycle(lst1) is False lst2 = n1 lst2.next = n2 n2.next = n3 n3.next = n2 assert DetectCycle(lst2) is n2
def Push(self, elem): node = Node(elem) node.next = self.top self.top = node self.length += 1
carry = digit_sum / 10 return result def sumLinkedListForward(first, second): first_list = reverseLinkedList(first) second_list = reverseLinkedList(second) return sumLinkedListReverse(first_list, second_list) def reverseLinkedList(linkedList): result = None while linkedList != None: data = linkedList.data if result == None: result = Node(data) else: node = Node(data) node.nextNode = result result = node linkedList = linkedList.nextNode return result first_test = Node(7) first_test.addToTail(1) first_test.addToTail(6) second_test = Node(5) second_test.addToTail(9) second_test.addToTail(2)
while node.nextNode != None: current = node.nextNode.data if current not in listSet: listSet.append(current) node = node.nextNode else: if node.nextNode.nextNode: node.nextNode = node.nextNode.nextNode else: node.nextNode = None def removeDuplicatesNoBuffer(node): if node is None: return None while node != None: current = node while current.nextNode != None: if current.nextNode.data == node.data: current.nextNode = current.nextNode.nextNode else: current = current.nextNode node = node.nextNode test = Node(3) test.addToTail(4) test.addToTail(5) test.addToTail(5) test.addToTail(3) test.addToTail(6)