Compare commits

...

1 Commits

73
GorkinMM/1.1.py Normal file
View File

@ -0,0 +1,73 @@
def create_node(name, phone):
return {'name': name, 'phone': phone, 'next': None}
def ll_insert(head, name, phone):
if head is None:
return create_node(name, phone)
current = head
while True:
if current['name'] == name:
current['phone'] = phone
return head
if current['next'] is None:
break
current = current['next']
current['next'] = create_node(name, phone)
return head
def ll_find(head, name):
current = head
while current:
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']:
if current['next']['name'] == name:
current['next'] = current['next']['next']
return head
current = current['next']
return head
def ll_list_all(head):
result = []
current = head
while current:
result.append((current['name'], current['phone']))
current = current['next']
result.sort(key=lambda x: x[0])
return result
if __name__ == "__main__":
phonebook = None
phonebook = ll_insert(phonebook, "Коля", "111-11-11")
phonebook = ll_insert(phonebook, "Саша", "222-22-22")
phonebook = ll_insert(phonebook, "Женя", "333-33-33")
print("После вставки:", ll_list_all(phonebook))
phonebook = ll_insert(phonebook, "Коля", "999-99-99")
print("После обновления Коля:", ll_list_all(phonebook))
print("Поиск Саша:", ll_find(phonebook, "Саша"))
print("Поиск Zoro:", ll_find(phonebook, "Zoro"))
phonebook = ll_delete(phonebook, "Саша")
print("Удалили Саша:", ll_list_all(phonebook))
phonebook = ll_delete(phonebook, "Коля")
print("Удалили Коля:", ll_list_all(phonebook))