37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
|
|
import linked_list as ll
|
||
|
|
|
||
|
|
def ht_create(size=100):
|
||
|
|
return [None] * size
|
||
|
|
|
||
|
|
def ht_get_hash(buckets, name):
|
||
|
|
return hash(name) % len(buckets)
|
||
|
|
|
||
|
|
def ht_insert(buckets, name, phone):
|
||
|
|
idx = ht_get_hash(buckets, name)
|
||
|
|
buckets[idx] = ll.ll_insert(buckets[idx], name, phone)
|
||
|
|
|
||
|
|
def ht_find(buckets, name):
|
||
|
|
idx = ht_get_hash(buckets, name)
|
||
|
|
return ll.ll_find(buckets[idx], name)
|
||
|
|
|
||
|
|
def ht_delete(buckets, name):
|
||
|
|
idx = ht_get_hash(buckets, name)
|
||
|
|
buckets[idx] = ll.ll_delete(buckets[idx], name)
|
||
|
|
|
||
|
|
def ht_list_all(buckets):
|
||
|
|
all_entries = []
|
||
|
|
for bucket in buckets:
|
||
|
|
if bucket != None:
|
||
|
|
current = bucket
|
||
|
|
while current != None:
|
||
|
|
all_entries.append((current['name'], current['phone']))
|
||
|
|
current = current['next']
|
||
|
|
|
||
|
|
for i in range(len(all_entries)):
|
||
|
|
for j in range(i + 1, len(all_entries)):
|
||
|
|
if all_entries[i][0] > all_entries[j][0]:
|
||
|
|
temp = all_entries[i]
|
||
|
|
all_entries[i] = all_entries[j]
|
||
|
|
all_entries[j] = temp
|
||
|
|
|
||
|
|
return all_entries
|