2026-rff_mp/romanovpv/task 1/docs/data/hash_table.py

29 lines
822 B
Python
Raw Normal View History

2026-05-03 19:39:03 +00:00
import linked_list as ll
2026-05-03 20:26:35 +00:00
def ht_create(size=100):
return [None] * size
2026-05-03 19:39:03 +00:00
2026-05-03 20:26:35 +00:00
def ht_get_hash(buckets, name):
return hash(name) % len(buckets)
2026-05-03 19:39:03 +00:00
def ht_insert(buckets, name, phone):
2026-05-03 20:26:35 +00:00
idx = ht_get_hash(buckets, name)
buckets[idx] = ll.ll_insert(buckets[idx], name, phone)
2026-05-03 19:39:03 +00:00
def ht_find(buckets, name):
2026-05-03 20:26:35 +00:00
idx = ht_get_hash(buckets, name)
return ll.ll_find(buckets[idx], name)
2026-05-03 19:39:03 +00:00
def ht_delete(buckets, name):
2026-05-03 20:26:35 +00:00
idx = ht_get_hash(buckets, name)
buckets[idx] = ll.ll_delete(buckets[idx], name)
2026-05-03 19:39:03 +00:00
def ht_list_all(buckets):
2026-05-03 20:26:35 +00:00
all_entries = []
for bucket in buckets:
if bucket:
current = bucket
while current:
all_entries.append((current['name'], current['phone']))
current = current['next']
return sorted(all_entries, key=lambda x: x[0])