Хеш-таблица
This commit is contained in:
parent
3cba71c6e7
commit
41433c287a
|
|
@ -46,4 +46,30 @@ def ll_list_all(head):
|
||||||
while current:
|
while current:
|
||||||
records.append((current['name'], current['phone']))
|
records.append((current['name'], current['phone']))
|
||||||
current = current['next']
|
current = current['next']
|
||||||
|
return sorted(records, key=lambda x: x[0])
|
||||||
|
def hash_func(name, size):
|
||||||
|
return sum(ord(c) for c in name) % size
|
||||||
|
|
||||||
|
def ht_create(size=1000):
|
||||||
|
return [None] * size
|
||||||
|
|
||||||
|
def ht_insert(table, name, phone):
|
||||||
|
idx = hash_func(name, len(table))
|
||||||
|
table[idx] = ll_insert(table[idx], name, phone)
|
||||||
|
|
||||||
|
def ht_find(table, name):
|
||||||
|
idx = hash_func(name, len(table))
|
||||||
|
return ll_find(table[idx], name)
|
||||||
|
|
||||||
|
def ht_delete(table, name):
|
||||||
|
idx = hash_func(name, len(table))
|
||||||
|
table[idx] = ll_delete(table[idx], name)
|
||||||
|
|
||||||
|
def ht_list_all(table):
|
||||||
|
records = []
|
||||||
|
for bucket in table:
|
||||||
|
current = bucket
|
||||||
|
while current:
|
||||||
|
records.append((current['name'], current['phone']))
|
||||||
|
current = current['next']
|
||||||
return sorted(records, key=lambda x: x[0])
|
return sorted(records, key=lambda x: x[0])
|
||||||
Loading…
Reference in New Issue
Block a user