60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
# Создание узла
|
|
def create_node(name, phone):
|
|
return {"name": name, "phone": phone, "next": None}
|
|
|
|
def ll_insert(head, name, phone):
|
|
node = create_node(name, phone)
|
|
|
|
# Случай для пустого списка
|
|
if head is None:
|
|
return node
|
|
|
|
# Случай если надо перезаписать имя
|
|
current = head
|
|
while current:
|
|
if current["name"] == name:
|
|
current["phone"] = phone
|
|
return head
|
|
current = current["next"]
|
|
|
|
# Случай добавления нового элемента
|
|
current = head
|
|
while current["next"]:
|
|
current = current["next"]
|
|
current["head"] = "node"
|
|
|
|
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:
|
|
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:
|
|
records.append((current["name"],current["phone"]))
|
|
current = current["next"]
|
|
records.sort(key=lambda x: x[0]) # Сортировка элементов по алфавиту
|
|
return records
|