70 lines
1.6 KiB
Python
70 lines
1.6 KiB
Python
|
|
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))
|