[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):
|
def create_node(name, phone):
|
||||||
return {"name": name, "phone": phone, "next": None}
|
return {"name": name, "phone": phone, "next": None}
|
||||||
|
|
||||||
def ll_insert(head, name, phone):
|
def ll_insert(head, name, phone):
|
||||||
node = create_node(name, phone)
|
node = create_node(name, phone)
|
||||||
|
|
||||||
# Случай для пустого списка
|
# Случай для пустого списка
|
||||||
if head is None:
|
if head is None:
|
||||||
return node
|
return node
|
||||||
|
|
||||||
# Случай если надо перезаписать имя
|
# Случай если надо перезаписать имя
|
||||||
current = head
|
current = head
|
||||||
while current:
|
while current:
|
||||||
|
|
@ -12,11 +16,13 @@ def ll_insert(head, name, phone):
|
||||||
current["phone"] = phone
|
current["phone"] = phone
|
||||||
return head
|
return head
|
||||||
current = current["next"]
|
current = current["next"]
|
||||||
|
|
||||||
# Случай добавления нового элемента
|
# Случай добавления нового элемента
|
||||||
current = head
|
current = head
|
||||||
while current["next"]:
|
while current["next"]:
|
||||||
current = current["next"]
|
current = current["next"]
|
||||||
current["head"] = "node"
|
current["head"] = "node"
|
||||||
|
|
||||||
def ll_find(head, name):
|
def ll_find(head, name):
|
||||||
current = head
|
current = head
|
||||||
while current:
|
while current:
|
||||||
|
|
@ -24,3 +30,30 @@ def ll_find(head, name):
|
||||||
return current["phone"]
|
return current["phone"]
|
||||||
current = current["next"]
|
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