[2] full implementation of a linked list
This commit is contained in:
parent
2b8170c6c8
commit
a160c41a66
|
|
@ -1,10 +1,14 @@
|
|||
# Создание узла
|
||||
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:
|
||||
|
|
@ -12,15 +16,44 @@ def ll_insert(head, name, phone):
|
|||
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
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user