Связный список
This commit is contained in:
parent
3eab4cc3aa
commit
52896e6f1d
53
KuzminskiyAA/Task 1/Dogs/Data/main.py
Normal file
53
KuzminskiyAA/Task 1/Dogs/Data/main.py
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
def ll_insert(head, name, phone):
|
||||||
|
"""Вставка в конец списка. Возвращает голову."""
|
||||||
|
new_node = {'name': name, 'phone': phone, 'next': None}
|
||||||
|
|
||||||
|
if head is None:
|
||||||
|
return new_node
|
||||||
|
|
||||||
|
current = head
|
||||||
|
while current['next']:
|
||||||
|
if current['name'] == name:
|
||||||
|
current['phone'] = phone
|
||||||
|
return head
|
||||||
|
current = current['next']
|
||||||
|
|
||||||
|
if current['name'] == name:
|
||||||
|
current['phone'] = phone
|
||||||
|
else:
|
||||||
|
current['next'] = new_node
|
||||||
|
|
||||||
|
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):
|
||||||
|
entries = []
|
||||||
|
current = head
|
||||||
|
while current:
|
||||||
|
entries.append((current['name'], current['phone']))
|
||||||
|
current = current['next']
|
||||||
|
entries.sort(key=lambda x: x[0])
|
||||||
|
return entries
|
||||||
Loading…
Reference in New Issue
Block a user