2026-rff_mp/MininaVD/docs/data/Laba1dop.txt
2026-05-16 08:11:42 +03:00

206 lines
5.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Linked List Phone Book:
def ll_insert(head, name, phone):
new_node = {'name': name, 'phone' : phone, 'next': None}
if head is None:
return new_node
if head['name'] == name:
head['phone'] = phone
return head
current = head
while current['next'] is not None:
if current['next']['name'] == name:
current['next']['phone'] = phone
return head
current = current['next']
current['next'] = new_node
return head
def ll_find(head, name):
current = head
while current != None:
if current['name']==name:
return current['phone']
current = current['next']
return None
def ll_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
current = head
while current['next'] is not None:
if current['next']['name'] == name:
current['next']==current['next']['next']
return head
current=current['next']
return head
def ll_list_all(head):
records= []
current = head
while current is not None:
records.append({'name': current['name'], 'phone': current['phone']})
current = current['next']
records.sort(key=lambda x: x['name'])
return records
def ll_print_all(head):
records = ll_list_all(head)
for record in records:
print(f"{record['name']}: {record['phone']}")
head = None
#добавление записей
print("\n1. Добавляем записи в список:")
head = ll_insert(head, "Анна", "123")
head = ll_insert(head, "Сергей", "234")
head = ll_insert(head, "Георгий", "345")
head = ll_insert(head, "Виктория", "456")
ll_print_all(head)
#Обновляем номер
print("\n2. Добавляем новую запись:")
head = ll_insert(head, "Антон", "666")
ll_print_all(head)
# Поиск записей
print(f"\n3. Производим поиск записей: \nПоиск Алисы: {ll_find(head, 'Анна')}")
print(f"Поиск Елены: {ll_find(head, 'Елена')}")
# Удаление записей
print("\n4. Удаляем Владимира:")
head = ll_delete(head, "Владимир")
ll_print_all(head)
print("\n5. Удаляем Антона:")
head = ll_delete(head, "Антон")
ll_print_all(head)
print("\n6. Пробуем удалить несуществующую запись:")
head = ll_delete(head, "Некто")
ll_print_all(head)
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
Tree function:
def bst_insert(root, name, phone):
if root is None:
return {'name': name, 'phone': phone, 'left': None, 'right': None}
if name < root['name']:
root['left'] = bst_insert(root['left'], name, phone)
elif name > root['name']:
root['right'] = bst_insert(root['right'], name, phone)
else:
root['phone'] = phone
return root
def bst_find(root, name):
current = root
while current is not None:
if name == current['name']:
return current['phone']
elif name < current['name']:
current = current['left']
else:
current = current['right']
return None
def bst_find_min(node):
current = node
while current['left'] is not None:
current = current['left']
return current
def bst_delete(root, name):
if root is None:
return None
if name < root['name']:
root['left'] = bst_delete(root['left'], name)
elif name > root['name']:
root['right'] = bst_delete(root['right'], name)
else:
if root['left'] is None:
return root['right']
elif root['right'] is None:
return root['left']
min_node = bst_find_min(root['right'])
root['name'] = min_node['name']
root['phone'] = min_node['phone']
root['right'] = bst_delete(root['right'], min_node['name'])
return root
def bst_list_all(root):
records = []
def inorder_traversal(node):
if node is not None:
inorder_traversal(node['left'])
records.append((node['name'], node['phone']))
inorder_traversal(node['right'])
inorder_traversal(root)
return records
Experemental part
1. Test data generation
def generate_records(count=10000):
records = []
for i in range(count):
name = f"User_{i:05d}"
phone = f"+7-{random.randint(100,999)}-{random.randint(100,999)}-{random.randint(1000,9999)}"
records.append((name, phone))
shuffled = records.copy()
random.shuffle(shuffled)
sorted_records = sorted(records, key=lambda x: x[0])
return shuffled, sorted_records