forked from UNN/2026-rff_mp
[1] add linked list phonebook implementation
This commit is contained in:
parent
197c266cd4
commit
b82a6e6f0d
46
KislyuninED/docks/data/1-st-exercize/main.py
Normal file
46
KislyuninED/docks/data/1-st-exercize/main.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# phonebook.py
|
||||
|
||||
# Узел списка: {'n': имя, 'p': телефон, 'nxt': следующий}
|
||||
def ll_insert(head, name, phone):
|
||||
# обновление, если уже есть
|
||||
curr = head
|
||||
while curr is not None:
|
||||
if curr['n'] == name:
|
||||
curr['p'] = phone
|
||||
return head
|
||||
curr = curr['nxt']
|
||||
# вставка в начало (новый узел становится головой)
|
||||
new_node = {'n': name, 'p': phone, 'nxt': head}
|
||||
return new_node
|
||||
|
||||
def ll_find(head, name):
|
||||
curr = head
|
||||
while curr is not None:
|
||||
if curr['n'] == name:
|
||||
return curr['p']
|
||||
curr = curr['nxt']
|
||||
return None
|
||||
|
||||
def ll_delete(head, name):
|
||||
if head is None:
|
||||
return None
|
||||
if head['n'] == name:
|
||||
return head['nxt']
|
||||
prev = head
|
||||
curr = head['nxt']
|
||||
while curr is not None:
|
||||
if curr['n'] == name:
|
||||
prev['nxt'] = curr['nxt']
|
||||
return head
|
||||
prev = curr
|
||||
curr = curr['nxt']
|
||||
return head
|
||||
|
||||
def ll_list_all(head):
|
||||
records = []
|
||||
curr = head
|
||||
while curr is not None:
|
||||
records.append((curr['n'], curr['p']))
|
||||
curr = curr['nxt']
|
||||
records.sort(key=lambda x: x[0])
|
||||
return records
|
||||
Loading…
Reference in New Issue
Block a user