38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
|
|
# Хеш-таблица на основе списка корзин, каждая корзина – связный список
|
|||
|
|
|
|||
|
|
import linked_list as ll
|
|||
|
|
|
|||
|
|
def create_hash_table(size=10):
|
|||
|
|
return [None] * size
|
|||
|
|
|
|||
|
|
def _hash_function(key, size):
|
|||
|
|
return hash(key) % size
|
|||
|
|
|
|||
|
|
def ht_insert(buckets, name, phone):
|
|||
|
|
idx = _hash_function(name, len(buckets))
|
|||
|
|
head = buckets[idx]
|
|||
|
|
new_head = ll.ll_insert(head, name, phone)
|
|||
|
|
buckets[idx] = new_head
|
|||
|
|
return buckets
|
|||
|
|
|
|||
|
|
def ht_find(buckets, name):
|
|||
|
|
idx = _hash_function(name, len(buckets))
|
|||
|
|
head = buckets[idx]
|
|||
|
|
return ll.ll_find(head, name)
|
|||
|
|
|
|||
|
|
def ht_delete(buckets, name):
|
|||
|
|
idx = _hash_function(name, len(buckets))
|
|||
|
|
head = buckets[idx]
|
|||
|
|
new_head = ll.ll_delete(head, name)
|
|||
|
|
buckets[idx] = new_head
|
|||
|
|
return buckets
|
|||
|
|
|
|||
|
|
def ht_list_all(buckets):
|
|||
|
|
all_records = []
|
|||
|
|
for head in buckets:
|
|||
|
|
current = head
|
|||
|
|
while current is not None:
|
|||
|
|
all_records.append((current['name'], current['phone']))
|
|||
|
|
current = current['next']
|
|||
|
|
all_records.sort(key=lambda x: x[0])
|
|||
|
|
return all_records
|