def test_iterate(): linked_list = LinkedList() for i in 'abc': linked_list.insert(i) j = -1 for i in linked_list: j += 1 assert i == 'abc'[::-1][j]
class AdjacencyList: def __init__(self, head: Vertex): self.head = head self.edgenodes = LinkedList() self.degree = 0 def connect(self, edge: Edge): self.edgenodes.insert(Edgenode(head=self.head, edge=edge)) self.degree += 1
def partition_list(list, number): greater_than_number_list = L.LinkedList() lesser_than_number_list = L.LinkedList() current = list.head while current is not None: if current.data > number: greater_than_number_list.add(current.data) else: lesser_than_number_list.add(current.data) current = current.next lesser_than_number_list_tail = lesser_than_number_list.head while lesser_than_number_list_tail.next is not None: lesser_than_number_list_tail = lesser_than_number_list_tail.next lesser_than_number_list_tail.next = greater_than_number_list.head return lesser_than_number_list
def sum_lists_forward(list_a, list_b): tail_a = list_a.seek_tail() tail_b = list_b.seek_tail() sum_list = LinkedList.LinkedList_Single() carry = 0 while tail_a is not None or tail_b is not None: if tail_a is None: temp_sum = tail_b.data + carry elif tail_b is None: temp_sum = tail_a.data + carry else: temp_sum = tail_a.data + tail_b.data + carry if temp_sum > 9: sum_list.add_node_to_head(temp_sum%10) carry = 1 else: sum_list.add_node_to_head(temp_sum) carry = 0 list_a.remove_tail() list_b.remove_tail() tail_a = list_a.seek_tail() tail_b = list_b.seek_tail() if carry != 0: sum_list.add_node_to_head(carry) return sum_list
def test_delete(): linked_list = LinkedList() abc = a, b, c = list('abc') for i in abc: linked_list.insert(i) linked_list.delete(b) assert linked_list.head.item is c assert linked_list.head.next.item is a assert linked_list.tail.item is a linked_list.delete(a) assert linked_list.head.item is c assert linked_list.head.next.item is None assert linked_list.tail.item is c linked_list.delete(c) assert linked_list.head.item is None assert linked_list.tail.item is None
def sum_lists_backward(list_a, list_b): current_a = list_a.head current_b = list_b.head # sum = [] sum = LinkedList.LinkedList_Single() carry = 0 while current_a is not None or current_b is not None: if current_a is None: temp_sum = current_b.data + carry elif current_b is None: temp_sum = current_a.data + carry else: temp_sum = current_a.data + current_b.data + carry if temp_sum > 9: # sum.append(temp_sum%10) sum.add_node_to_list(temp_sum%10) carry = 1 else: # sum.append(temp_sum) sum.add_node_to_list(temp_sum) carry = 0 if current_a: current_a = current_a.next if current_b: current_b = current_b.next if carry != 0: # sum.append(carry) sum.add_node_to_list(carry) return sum
def remove_duplicates_without_buffer(list): current = L.SingleLinkedListNode("") current.next = list.head while current.next is not None: inner_current = current.next while inner_current.next is not None: if current.next.data == inner_current.next.data: inner_current.next = inner_current.next.next else: inner_current = inner_current.next current = current.next
def insert(self, key, value): hashval = hash(key) % len(self.hash_table) curr = self.hash_table[hashval].root self.hash_table[hashval].root.value = (None, None) while curr.value[0] != key and curr.next != None: curr = curr.next if curr.value[0] == key: curr.value[1] == value else: curr.next = LinkedList.Node((key, value)) self.hash_table[hashval].end = curr.next
def test_search_fail(): linked_list = LinkedList() abc = a, b, c = list('abc') for i in abc: linked_list.insert(i) with pytest.raises(FileNotFoundError): linked_list.search('d')
def test_previous(): linked_list = LinkedList() abc = a, b, c = list('abc') for i in abc: linked_list.insert(i) assert linked_list.previous(a).item is b assert linked_list.previous(b).item is c assert linked_list.previous(c).item is None
def test_search(): linked_list = LinkedList() abc = a, b, c = list('abc') for i in abc: linked_list.insert(i) assert linked_list.search(a).item is a assert linked_list.search(b).item is b assert linked_list.search(c).item is c
def list_of_depths(children, result_list): if len(children) is 0: return same_depth_list = LinkedList.LinkedList_Single() for child in children: same_depth_list.add_node_to_list(child) result_list.append(same_depth_list) children_of_children = [] current_node = same_depth_list.head while current_node is not None: if current_node.data.left_child is not None: children_of_children.append(current_node.data.left_child) if current_node.data.right_child is not None: children_of_children.append(current_node.data.right_child) current_node = current_node.next list_of_depths(children_of_children, result_list)
def test_insert(): linked_list = LinkedList() a, b = list('ab') assert linked_list.head.item is None assert linked_list.tail.item is None linked_list.insert(a) assert linked_list.head.item is a assert linked_list.head.next.item is None assert linked_list.tail.item is a linked_list.insert(b) assert linked_list.head.item is b assert linked_list.head.next.item is a assert linked_list.tail.item is a
def remove_duplicates_without_buffer(list): current = L.SingleLinkedListNode("") current.next = list.head while current.next is not None: inner_current = current.next while inner_current.next is not None: if current.next.data == inner_current.next.data: inner_current.next = inner_current.next.next else: inner_current = inner_current.next current = current.next if __name__ == '__main__': linked_list = L.LinkedList() linked_list.add_array_of_elements_to_list( [34, 1, 2, 1, 4, 5, 34, 6, 7, 34]) linked_list.print_list() linked_list.add(23) linked_list.print_list() linked_list.remove_element_from_list(4) linked_list.print_list() remove_duplicates_without_buffer(linked_list) linked_list.print_list() single_linked_list = L.LinkedList_Single() single_linked_list.add_array_of_data_to_linked_list([1, 1, 1, 1]) single_linked_list.print_list() single_linked_list.add_array_of_data_to_linked_list([1, 1, 1, 1, 1]) single_linked_list.print_list()
from datastructures import LinkedList def is_looped(list): current = list.head traversed_nodes = [] traversed_nodes.append(current) while current.next is not None: current = current.next if current in traversed_nodes: return True traversed_nodes.append(current) return False if __name__ == '__main__': list_a = LinkedList.LinkedList_Single() list_a.add_array_of_data_to_linked_list([1, 2, 3, 4, 5, 6, 7]) list_a.print_list() # list_a.seek_tail().next = list_a.head.next.next print(is_looped(list_a))
print 'Wrong parameter provided, you asked to find 0 element' return first_node = alist.head last_node = alist.head # build a window spaced N for i in range(1, n): if last_node.next is None: print 'There are only {} in the list'.format(i) print alist return else: last_node = last_node.next # Move both pointers until we get to the end of the list while last_node.next is not None: last_node = last_node.next first_node = first_node.next # print N values found print 'Last {} elements in the list: '.format(n), for i in range(1, n + 1): print first_node.value, first_node = first_node.next # Build a list with 10 elements for testing ll = LinkedList() for i in range(1, 21): ll.insert(random.randint(1, 100)) print 'Given list:', print ll find_last_N(ll, 1)
def __init__(self, head: Vertex): self.head = head self.edgenodes = LinkedList() self.degree = 0
def test_delete_fail(): linked_list = LinkedList() for i in 'abc': linked_list.insert(i) with pytest.raises(FileNotFoundError): linked_list.delete('d')
from datastructures import LinkedList as L def partition_list(list, number): greater_than_number_list = L.LinkedList() lesser_than_number_list = L.LinkedList() current = list.head while current is not None: if current.data > number: greater_than_number_list.add(current.data) else: lesser_than_number_list.add(current.data) current = current.next lesser_than_number_list_tail = lesser_than_number_list.head while lesser_than_number_list_tail.next is not None: lesser_than_number_list_tail = lesser_than_number_list_tail.next lesser_than_number_list_tail.next = greater_than_number_list.head return lesser_than_number_list if __name__ == '__main__': linked_list = L.LinkedList() linked_list.add_array_of_elements_to_list( [34, 355, 124, 656, 123, 767, 34, 542, 123]) partitioned_list = partition_list(linked_list, 125) partitioned_list.print_list()
def __init__(self, hash_table_size=19): self.hash_table = [LinkedList() for _ in range(hash_table_size)]
def linked_list(): return LinkedList()