import random # Sv Spisok ll_head = None def ll_insert(name, phone): global ll_head cur = ll_head while cur is not None: if cur['name'] == name: cur['phone'] = phone return cur = cur['next'] new_node = {'name': name, 'phone': phone, 'next': ll_head} ll_head = new_node def ll_find(name): cur = ll_head while cur is not None: if cur['name'] == name: return cur['phone'] cur = cur['next'] return None def ll_delete(name): global ll_head if ll_head is None: return if ll_head['name'] == name: ll_head = ll_head['next'] return prev = ll_head cur = ll_head['next'] while cur is not None: if cur['name'] == name: prev['next'] = cur['next'] return prev = cur cur = cur['next'] def ll_list_all(): records = [] cur = ll_head while cur is not None: records.append((cur['name'], cur['phone'])) cur = cur['next'] records.sort(key=lambda x: x[0]) return records # HESH BUCKET_COUNT = 10 buckets = [None] * BUCKET_COUNT def hash_func(name): s = 0 for ch in name: s += ord(ch) return s % BUCKET_COUNT def ht_insert(name, phone): global buckets idx = hash_func(name) cur = buckets[idx] while cur is not None: if cur['name'] == name: cur['phone'] = phone return cur = cur['next'] new_node = {'name': name, 'phone': phone, 'next': buckets[idx]} buckets[idx] = new_node def ht_find(name): idx = hash_func(name) cur = buckets[idx] while cur is not None: if cur['name'] == name: return cur['phone'] cur = cur['next'] return None def ht_delete(name): global buckets idx = hash_func(name) head = buckets[idx] if head is None: return if head['name'] == name: buckets[idx] = head['next'] return prev = head cur = head['next'] while cur is not None: if cur['name'] == name: prev['next'] = cur['next'] return prev = cur cur = cur['next'] def ht_list_all(): all_records = [] for head in buckets: cur = head while cur is not None: all_records.append((cur['name'], cur['phone'])) cur = cur['next'] all_records.sort(key=lambda x: x[0]) return all_records #Test if __name__ == '__main__': print("=== Тест связного списка ===") ll_insert("Kiril", "123-456") ll_insert("Pavel", "789-012") ll_insert("Alina", "345-678") ll_insert("Kolia", "111-222") # обновление print("Все записи (список):", ll_list_all()) print("Поиск Kiril:", ll_find("Kiril")) ll_delete("Pavel") print("После удаления Pavel:", ll_list_all()) print("\n=== Тест хеш-таблицы ===") ht_insert("Kiril", "123-456") ht_insert("Pavel", "789-012") ht_insert("Alina", "345-678") ht_insert("Kolia", "111-222") print("Все записи (хеш):", ht_list_all()) print("Поиск Anna:", ht_find("Kiril")) ht_delete("Kolia") print("После удаления Kolia:", ht_list_all())