[1] add hash table with fixed bucket count

This commit is contained in:
KislyuninED 2026-05-14 00:59:11 +00:00
parent b82a6e6f0d
commit 2052f2c4b8

View File

@ -43,4 +43,42 @@ def ll_list_all(head):
records.append((curr['n'], curr['p'])) records.append((curr['n'], curr['p']))
curr = curr['nxt'] curr = curr['nxt']
records.sort(key=lambda x: x[0]) records.sort(key=lambda x: x[0])
return records return records
# хеш-функция сумма ord(name) % size
def _hash(name, size):
h = 0
for ch in name:
h += ord(ch)
return h % size
SIZE = 13 # фиксированный размер таблицы
def ht_create():
return [None] * SIZE
def ht_insert(buckets, name, phone):
idx = _hash(name, len(buckets))
buckets[idx] = ll_insert(buckets[idx], name, phone)
return buckets
def ht_find(buckets, name):
idx = _hash(name, len(buckets))
return ll_find(buckets[idx], name)
def ht_delete(buckets, name):
idx = _hash(name, len(buckets))
buckets[idx] = ll_delete(buckets[idx], name)
return buckets
def ht_list_all(buckets):
all_records = []
for head in buckets:
curr = head
while curr:
all_records.append((curr['n'], curr['p']))
curr = curr['nxt']
all_records.sort(key=lambda x: x[0])
return all_records