[1] Initial linked list implementation for phonebook

This commit is contained in:
SavelevMI 2026-05-21 13:34:23 +00:00
parent 3e175eb367
commit c19fa42056

View File

@ -0,0 +1,58 @@
def create_node(name, phone):
return {'name': name, 'phone': phone, 'next': None}
def ll_insert(head, name, phone):
current = head
# Поиск существующей записи
while current is not None:
if current['name'] == name:
current['phone'] = phone
return head
current = current['next']
# Создание нового узла
new_node = create_node(name, phone)
if head is None:
return new_node
current = head
while current['next'] is not None:
current = current['next']
current['next'] = new_node
return head
def ll_find(head, name):
current = head
while current is not 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']
prev = head
current = head['next']
while current is not None:
if current['name'] == name:
prev['next'] = current['next']
return head
prev = current
current = current['next']
return head
def ll_list_all(head):
records = []
current = head
while current is not None:
records.append((current['name'], current['phone']))
current = current['next']
records.sort(key=lambda pair: pair[0])
return records