Добавил 1.1.Связаный список

This commit is contained in:
SolovevDD 2026-02-28 00:29:13 +03:00
parent ddf8ef5105
commit 089b6906b9

70
SolovevDD/zadanie11.py Normal file
View File

@ -0,0 +1,70 @@
def ll_insert(head, name, phone):
if head is None:
return {'name': name, 'phone': phone, 'next': None}
curr = head
while True:
if curr['name'] == name:
curr['phone'] = phone
return head
if curr['next'] is None:
break
curr = curr['next']
curr['next'] = {'name': name, 'phone': phone, 'next': None}
return head
def ll_find(head, name):
curr = head
while curr is not None:
if curr['name'] == name:
return curr['phone']
curr = curr['next']
return None
def ll_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
curr = head
while curr['next'] is not None:
if curr['next']['name'] == name:
curr['next'] = curr['next']['next']
return head
curr = curr['next']
return head
def ll_list_all(head):
res = []
curr = head
while curr is not None:
res.append((curr['name'], curr['phone']))
curr = curr['next']
res.sort(key=lambda x: x[0])
return res
if __name__ == "__main__":
book = None
book = ll_insert(book, "Маша", "8900111")
book = ll_insert(book, "Саша", "8900222")
book = ll_insert(book, "Даша", "8900333")
print(ll_list_all(book))
book = ll_insert(book, "Маша", "0000000")
print(ll_list_all(book))
print(ll_find(book, "Саша"))
print(ll_find(book, "Петя"))
book = ll_delete(book, "Маша")
book = ll_delete(book, "Саша")
print(ll_list_all(book))