forked from UNN/2026-rff_mp
Delete src/linked_list.py
This commit is contained in:
parent
4fd3db10ee
commit
5fbe055481
|
|
@ -1,74 +0,0 @@
|
||||||
# linked_list.py
|
|
||||||
# Связный список для телефонного справочника
|
|
||||||
|
|
||||||
def create_node(name, phone):
|
|
||||||
"""Создаёт новый узел-словарь."""
|
|
||||||
return {'name': name, 'phone': phone, 'next': None}
|
|
||||||
|
|
||||||
def ll_insert(head, name, phone):
|
|
||||||
"""
|
|
||||||
Вставляет или обновляет запись.
|
|
||||||
Если имя уже существует – обновляет телефон.
|
|
||||||
Если нет – добавляет в конец списка.
|
|
||||||
Возвращает голову списка (может измениться, если вставка в начало).
|
|
||||||
"""
|
|
||||||
# Если список пуст – создаём первый узел
|
|
||||||
if head is None:
|
|
||||||
return create_node(name, phone)
|
|
||||||
|
|
||||||
# Проверяем, не находится ли имя в первом узле
|
|
||||||
if head['name'] == name:
|
|
||||||
head['phone'] = phone
|
|
||||||
return head
|
|
||||||
|
|
||||||
# Ищем узел с таким именем или конец списка
|
|
||||||
current = head
|
|
||||||
while current['next'] is not None:
|
|
||||||
if current['next']['name'] == name:
|
|
||||||
current['next']['phone'] = phone
|
|
||||||
return head
|
|
||||||
current = current['next']
|
|
||||||
|
|
||||||
# Имя не найдено – добавляем в конец
|
|
||||||
current['next'] = create_node(name, phone)
|
|
||||||
return head
|
|
||||||
|
|
||||||
def ll_find(head, name):
|
|
||||||
"""Ищет телефон по имени. Возвращает phone или None."""
|
|
||||||
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):
|
|
||||||
"""
|
|
||||||
Возвращает список всех записей в виде [(name, phone), ...],
|
|
||||||
отсортированный по имени. Сама структура не сортируется.
|
|
||||||
"""
|
|
||||||
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 record
|
|
||||||
Loading…
Reference in New Issue
Block a user