51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
|
|
def ll_list_all(start: dict | None) -> list:
|
||
|
|
items = []
|
||
|
|
node = start
|
||
|
|
while node is not None:
|
||
|
|
items.append((node['name'], node['phone']))
|
||
|
|
node = node['next']
|
||
|
|
items.sort(key=lambda x: x[0])
|
||
|
|
return items
|
||
|
|
|
||
|
|
|
||
|
|
def ll_delete(start: dict | None, key: str) -> dict | None:
|
||
|
|
if start is None:
|
||
|
|
return None
|
||
|
|
|
||
|
|
if start['name'] == key:
|
||
|
|
return start['next']
|
||
|
|
|
||
|
|
ptr = start
|
||
|
|
while ptr['next'] is not None:
|
||
|
|
if ptr['next']['name'] == key:
|
||
|
|
ptr['next'] = ptr['next']['next']
|
||
|
|
return start
|
||
|
|
ptr = ptr['next']
|
||
|
|
|
||
|
|
return start
|
||
|
|
|
||
|
|
|
||
|
|
def ll_find(start: dict | None, key: str) -> str | None:
|
||
|
|
ptr = start
|
||
|
|
while ptr is not None:
|
||
|
|
if ptr['name'] == key:
|
||
|
|
return ptr['phone']
|
||
|
|
ptr = ptr['next']
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def ll_insert(start: dict | None, key: str, value: str) -> dict:
|
||
|
|
if start is None:
|
||
|
|
return {'name': key, 'phone': value, 'next': None}
|
||
|
|
|
||
|
|
ptr = start
|
||
|
|
while True:
|
||
|
|
if ptr['name'] == key:
|
||
|
|
ptr['phone'] = value
|
||
|
|
return start
|
||
|
|
|
||
|
|
if ptr['next'] is None:
|
||
|
|
ptr['next'] = {'name': key, 'phone': value, 'next': None}
|
||
|
|
return start
|
||
|
|
|
||
|
|
ptr = ptr['next']
|