def list_min_heap_union(heap1, heap2): if heap1.head is None: return heap2 if heap2.head is None: return heap1 if heap1.head.key < heap2.head.key: new_min = heap1.head else: new_min = heap2.head heap1_keys = [] x = heap1.head while x is not None: heap1_keys.append(x.key) x = x.next if heap1_keys: hash_table, h = perfect_hashing_init(Array(heap1_keys)) x = heap2.head while x is not None: y = x.next if heap1_keys == [] or perfect_hashing_search(hash_table, x.key, h) is None: singly_linked_list_insert(heap1, x) x = y singly_linked_list_delete(heap1, new_min) singly_linked_list_insert(heap1, new_min) return heap1
def singly_linked_list_reverse(L): L_ = List() L_.head = None while L.head is not None: x = L.head singly_linked_list_delete(L, L.head) singly_linked_list_insert(L_, x) L.head = L_.head
def test_singly_linked_list_insert(self): list_, nodes, keys = get_random_singly_linked_list() new_key = random.randint(0, 999) new_node = SNode(new_key) singly_linked_list_insert(list_, new_node) actual_keys = get_linked_list_keys(list_) expected_keys = [new_key] + keys assert_that(actual_keys, is_(equal_to(expected_keys)))
def list_min_heap_insert(heap, key): x = SNode(key) if heap.head is None or key < heap.head.key: singly_linked_list_insert(heap, x) if heap.tail is None: heap.tail = x else: x.next = heap.head.next heap.head.next = x if heap.tail is heap.head: heap.tail = x
def josephus_simulate(n, m): L = List() singly_linked_list_insert(L, SNode(n)) x = L.head for i in rbetween(n - 1, 1): singly_linked_list_insert(L, SNode(i)) x.next = L.head for i in between(1, n): for j in between(1, m): x = x.next print(x.next.key) if L.head is x.next: L.head = x.next.next x.next = x.next.next
def list_heap_extract_min(heap): if heap.head is None: raise RuntimeError('heap underflow') min = heap.head.key heap.head = heap.head.next if heap.head is None: heap.tail = None return min x = new_min = heap.head while x is not None: if x.key < new_min.key: new_min = x x = x.next singly_linked_list_delete(heap, new_min) singly_linked_list_insert(heap, new_min) x = heap.head while x.next is not None: x = x.next heap.tail = x return min
def singly_linked_list_push(L, k): x = SNode(None) x.key = k singly_linked_list_insert(L, x)