forked from UNN/2026-rff_mp
Перемещены файлы и добавлена реализация LinkedList
This commit is contained in:
parent
9c1cd94e87
commit
b984ec3569
|
|
@ -1,25 +0,0 @@
|
|||
"""
|
||||
Связный список (LinkedListPhoneBook)
|
||||
|
||||
Узел представляется словарём:
|
||||
{'name': 'Имя', 'phone': '123', 'next': None}.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
"""
|
||||
Проходит до конца (или сразу добавляет в конец) и возвращает новую
|
||||
голову (если вставка в начало) или изменяет список по ссылке.
|
||||
Удобнее возвращать новую голову, если вставка может быть в начало.
|
||||
"""
|
||||
|
||||
def ll_find(head, name):
|
||||
"""Ищет узел, возвращает телефон или None."""
|
||||
|
||||
def ll_delete(head, name):
|
||||
"""Удаляет узел, возвращает новую голову."""
|
||||
|
||||
def ll_list_all(head):
|
||||
"""Cобирает все записи в список и сортирует.
|
||||
сортировка вынесена отдельно)."""
|
||||
0
MusinAA/task1/__init__.py
Normal file
0
MusinAA/task1/__init__.py
Normal file
60
MusinAA/task1/structures/LinkedList.py
Normal file
60
MusinAA/task1/structures/LinkedList.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
"""
|
||||
Связный список (LinkedListPhoneBook)
|
||||
|
||||
Узел представляется словарём:
|
||||
{'name': 'Имя', 'phone': '123', 'next': None}.
|
||||
"""
|
||||
|
||||
|
||||
def ll_insert(head : dict|None, name: str, phone: str) -> dict:
|
||||
"""
|
||||
Проходит до конца (или сразу добавляет в конец) и возвращает новую
|
||||
голову (если вставка в начало) или изменяет список по ссылке.
|
||||
Удобнее возвращать новую голову, если вставка может быть в начало.
|
||||
"""
|
||||
|
||||
newNode = {'name': name, 'phone': phone, 'next': None}
|
||||
if head == None:
|
||||
return newNode
|
||||
|
||||
currentNode = head
|
||||
while currentNode['next'] != None:
|
||||
currentNode = currentNode['next']
|
||||
currentNode['next'] = newNode
|
||||
return head
|
||||
|
||||
def ll_find(head : dict|None, name: str) -> str|None:
|
||||
"""Ищет узел, возвращает телефон или None."""
|
||||
currentNode = head
|
||||
while currentNode != None:
|
||||
if currentNode['name'] == name:
|
||||
return currentNode['phone']
|
||||
currentNode = currentNode['next']
|
||||
return None
|
||||
|
||||
def ll_delete(head : dict|None, name: str) -> dict:
|
||||
"""Удаляет узел, возвращает новую голову."""
|
||||
if head == None:
|
||||
return None
|
||||
|
||||
if head['name'] == name:
|
||||
return head['next']
|
||||
|
||||
currentNode = head
|
||||
while currentNode['next'] != None:
|
||||
if currentNode['next']['name'] == name:
|
||||
currentNode['next'] = currentNode['next']['next']
|
||||
return head
|
||||
currentNode = currentNode['next']
|
||||
return head
|
||||
|
||||
def ll_list_all(head: dict|None) -> list:
|
||||
"""Cобирает все записи в список и сортирует.
|
||||
сортировка вынесена отдельно)."""
|
||||
records = []
|
||||
currentNode = head
|
||||
while currentNode != None:
|
||||
records.append((currentNode['name'], currentNode['phone']))
|
||||
currentNode = currentNode['next']
|
||||
records.sort(key=lambda item: item[0])
|
||||
return records
|
||||
0
MusinAA/task1/structures/__init__.py
Normal file
0
MusinAA/task1/structures/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user