добавлена реализация хеш-таблицы
This commit is contained in:
parent
b07adc7cb4
commit
76b52b99e4
|
|
@ -64,3 +64,41 @@ def ll_list_all(head):
|
||||||
current = current['next']
|
current = current['next']
|
||||||
records.sort(key=lambda x: x[0])
|
records.sort(key=lambda x: x[0])
|
||||||
return records
|
return records
|
||||||
|
|
||||||
|
# 2. Hash Function
|
||||||
|
|
||||||
|
def hash_function(name, table_size):
|
||||||
|
return sum(ord(c) for c in name) % table_size
|
||||||
|
|
||||||
|
|
||||||
|
def ht_create(size=1000):
|
||||||
|
return [None] * size
|
||||||
|
|
||||||
|
|
||||||
|
def ht_insert(buckets, name, phone):
|
||||||
|
size = len(buckets)
|
||||||
|
index = hash_function(name, size)
|
||||||
|
buckets[index] = ll_insert(buckets[index], name, phone)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_find(buckets, name):
|
||||||
|
size = len(buckets)
|
||||||
|
index = hash_function(name, size)
|
||||||
|
return ll_find(buckets[index], name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_delete(buckets, name):
|
||||||
|
size = len(buckets)
|
||||||
|
index = hash_function(name, size)
|
||||||
|
buckets[index] = ll_delete(buckets[index], name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_list_all(buckets):
|
||||||
|
records = []
|
||||||
|
for bucket in buckets:
|
||||||
|
current = bucket
|
||||||
|
while current is not None:
|
||||||
|
records.append((current['name'], current['phone']))
|
||||||
|
current = current['next']
|
||||||
|
records.sort(key=lambda x: x[0])
|
||||||
|
return records
|
||||||
Loading…
Reference in New Issue
Block a user