[1] add hash table implementation
This commit is contained in:
parent
e464e4e1e0
commit
0c2e487472
|
|
@ -1,28 +1,19 @@
|
||||||
|
|
||||||
def ll_insert(head, name, phone):
|
def ll_insert(head, name, phone):
|
||||||
current = head
|
current = head
|
||||||
# Проверяем, существует ли уже такой абонент
|
|
||||||
while current is not None:
|
while current is not None:
|
||||||
if current['name'] == name:
|
if current['name'] == name:
|
||||||
current['phone'] = phone
|
current['phone'] = phone
|
||||||
return head
|
return head
|
||||||
current = current['next']
|
current = current['next']
|
||||||
|
|
||||||
# Создаём новый узел
|
|
||||||
new_node = {'name': name, 'phone': phone, 'next': None}
|
new_node = {'name': name, 'phone': phone, 'next': None}
|
||||||
|
|
||||||
# Если список пуст, новый узел становится головой
|
|
||||||
if head is None:
|
if head is None:
|
||||||
return new_node
|
return new_node
|
||||||
|
|
||||||
# Иначе проходим в конец и вставляем
|
|
||||||
current = head
|
current = head
|
||||||
while current['next'] is not None:
|
while current['next'] is not None:
|
||||||
current = current['next']
|
current = current['next']
|
||||||
current['next'] = new_node
|
current['next'] = new_node
|
||||||
return head
|
return head
|
||||||
|
|
||||||
|
|
||||||
def ll_find(head, name):
|
def ll_find(head, name):
|
||||||
current = head
|
current = head
|
||||||
while current is not None:
|
while current is not None:
|
||||||
|
|
@ -31,15 +22,11 @@ def ll_find(head, name):
|
||||||
current = current['next']
|
current = current['next']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def ll_delete(head, name):
|
def ll_delete(head, name):
|
||||||
if head is None:
|
if head is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Если удаляемый элемент — голова
|
|
||||||
if head['name'] == name:
|
if head['name'] == name:
|
||||||
return head['next']
|
return head['next']
|
||||||
|
|
||||||
prev = head
|
prev = head
|
||||||
current = head['next']
|
current = head['next']
|
||||||
while current is not None:
|
while current is not None:
|
||||||
|
|
@ -50,7 +37,6 @@ def ll_delete(head, name):
|
||||||
current = current['next']
|
current = current['next']
|
||||||
return head
|
return head
|
||||||
|
|
||||||
|
|
||||||
def ll_list_all(head):
|
def ll_list_all(head):
|
||||||
records = []
|
records = []
|
||||||
current = head
|
current = head
|
||||||
|
|
@ -61,20 +47,55 @@ def ll_list_all(head):
|
||||||
return records
|
return records
|
||||||
|
|
||||||
|
|
||||||
# --------------------- Тестирование связного списка ---------------------
|
HASH_SIZE = 997
|
||||||
|
|
||||||
|
def hash_func(name, size):
|
||||||
|
return hash(name) % size
|
||||||
|
|
||||||
|
def ht_create():
|
||||||
|
return [None] * HASH_SIZE
|
||||||
|
|
||||||
|
def ht_insert(table, name, phone):
|
||||||
|
idx = hash_func(name, len(table))
|
||||||
|
table[idx] = ll_insert(table[idx], name, phone)
|
||||||
|
return table
|
||||||
|
|
||||||
|
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)
|
||||||
|
return table
|
||||||
|
|
||||||
|
def ht_list_all(table):
|
||||||
|
all_records = []
|
||||||
|
for head in table:
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("=== Тестирование связного списка ===")
|
print("=== Тестирование связного списка ===")
|
||||||
head = None
|
head = None
|
||||||
|
|
||||||
head = ll_insert(head, "Ivan", "123-456")
|
head = ll_insert(head, "Ivan", "123-456")
|
||||||
head = ll_insert(head, "Boris", "789-012")
|
head = ll_insert(head, "Boris", "789-012")
|
||||||
head = ll_insert(head, "Anna", "345-678")
|
head = ll_insert(head, "Anna", "345-678")
|
||||||
head = ll_insert(head, "Ivan", "111-222") # обновление
|
head = ll_insert(head, "Ivan", "111-222")
|
||||||
|
print("LinkedList:", ll_list_all(head))
|
||||||
|
|
||||||
print("Все записи:", ll_list_all(head))
|
print("\n=== Тестирование хеш-таблицы ===")
|
||||||
print("Телефон Ivan:", ll_find(head, "Ivan"))
|
ht = ht_create()
|
||||||
print("Телефон Boris:", ll_find(head, "Boris"))
|
ht = ht_insert(ht, "Ivan", "123-456")
|
||||||
print("Телефон Noname:", ll_find(head, "Noname"))
|
ht = ht_insert(ht, "Boris", "789-012")
|
||||||
|
ht = ht_insert(ht, "Anna", "345-678")
|
||||||
head = ll_delete(head, "Boris")
|
ht = ht_insert(ht, "Ivan", "111-222")
|
||||||
print("После удаления Boris, все записи:", ll_list_all(head))
|
print("HashTable entries:", ht_list_all(ht))
|
||||||
|
print("Find Ivan:", ht_find(ht, "Ivan"))
|
||||||
|
ht = ht_delete(ht, "Boris")
|
||||||
|
print("After delete Boris:", ht_list_all(ht))
|
||||||
Loading…
Reference in New Issue
Block a user