feat: implement linked list

This commit is contained in:
lomakinae 2026-04-13 01:36:00 +03:00
parent 6e4ae1835b
commit a741e56d3c
3 changed files with 52 additions and 0 deletions

2
lomakinae/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__/
*.pyc

View File

View File

@ -0,0 +1,50 @@
def _ll_new_node(name, phone):
return {'name': name, 'phone': phone, 'next': None}
def ll_insert(head, name, phone):
node = head
while node is not None:
if node['name'] == name:
node['phone'] = phone
return head
node = node['next']
new_node = _ll_new_node(name, phone)
new_node['next'] = head
return new_node
def ll_find(head, name):
node = head
while node is not None:
if node['name'] == name:
return node['phone']
node = node['next']
return None
def ll_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
node = head
while node['next'] is not None:
if node['next']['name'] == name:
node['next'] = node['next']['next']
return head
node = node['next']
return head
def ll_list_all(head):
records = []
node = head
while node is not None:
records.append((node['name'], node['phone']))
node = node['next']
return sorted(records)