1
0
forked from UNN/2026-rff_mp
2026-rff_mp/KorotkinSE/task1/linkedlist.py
2026-09-23 11:53:32 +03:00

58 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Node:
def __init__(self, name, phone):
self.name = name
self.phone = phone
self.next = None
def ll_insert(head, name, phone):
"""Обновление записи или вставка в конец списка"""
if head is None:
return Node(name, phone)
current = head
while current is not None:
if current.name == name:
current.phone = phone
return head
if current.next is None:
break
current = current.next
current.next = Node(name, phone)
return head
def ll_find(head, name):
"""Поиск телефона по имени"""
current = head
while current is not None:
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 is not None:
if current.next.name == name:
current.next = current.next.next
return head
current = current.next
return head
def ll_list_all(head):
"""Сбор всех записей и сортировка по имени"""
records = []
current = head
while current is not None:
records.append((current.name, current.phone))
current = current.next
records.sort(key=lambda x: x[0])
return records