2026-rff_mp/vinichukan/src/linked_list.py

58 lines
1.1 KiB
Python
Raw Normal View History

def ll_insert(head, name, phone):
new_node = {'name': name, 'phone': phone, 'next': None}
if head is None:
return new_node
cur = head
prev = None
while cur is not None:
if cur['name'] == name:
cur['phone'] = phone
return head
prev = cur
cur = cur['next']
prev['next'] = new_node
return head
def ll_find(head, name):
cur = head
while cur is not None:
if cur['name'] == name:
return cur['phone']
cur = cur['next']
return None
def ll_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
prev = head
cur = head['next']
while cur is not None:
if cur['name'] == name:
prev['next'] = cur['next']
return head
prev = cur
cur = cur['next']
return head
def ll_list_all(head):
result = []
cur = head
while cur is not None:
result.append((cur['name'], cur['phone']))
cur = cur['next']
result.sort(key=lambda x: x[0])
return result