2026-rff_mp/pogodinda/lab1/hash_table_phonebook.py
2026-05-21 20:29:55 +03:00

31 lines
912 B
Python

from linked_list_phonebook import ll_insert, ll_find, ll_delete, ll_list_all
def hash_function(name, table_size):
hash_value = 0
for char in name:
hash_value = (hash_value * 31 + ord(char)) % table_size
return hash_value
def create_hash_table(size=1000):
return [None] * size
def ht_insert(buckets, name, phone):
index = hash_function(name, len(buckets))
buckets[index] = ll_insert(buckets[index], name, phone)
def ht_find(buckets, name):
index = hash_function(name, len(buckets))
return ll_find(buckets[index], name)
def ht_delete(buckets, name):
index = hash_function(name, len(buckets))
buckets[index] = ll_delete(buckets[index], name)
def ht_list_all(buckets):
all_records = []
for head in buckets:
if head is not None:
all_records.extend(ll_list_all(head))
all_records.sort(key=lambda x: x[0])
return all_records