diff --git a/meosyam/docs/data/1/phonebook.py b/meosyam/docs/data/1/phonebook.py new file mode 100644 index 00000000..f362b462 --- /dev/null +++ b/meosyam/docs/data/1/phonebook.py @@ -0,0 +1,59 @@ +def ll_insert(head, name, phone): + cur = head + while cur: + if cur['name'] == name: + cur['phone'] = phone + return head + cur = cur['next'] + new_node = {'name': name, 'phone': phone, 'next': None} + if head is None: + return new_node + cur = head + while cur['next']: + cur = cur['next'] + cur['next'] = new_node + return head + +def ll_find(head, name): + cur = head + while cur: + if cur['name'] == name: + return cur['phone'] + cur = cur['next'] + return None + +def ll_delete(head, name): + if head is None: + return None + if head['name'] == name: + return head['next'] + prev = head + cur = head['next'] + while cur: + if cur['name'] == name: + prev['next'] = cur['next'] + return head + prev = cur + cur = cur['next'] + return head + +def ll_list_all(head): + res = [] + cur = head + while cur: + res.append((cur['name'], cur['phone'])) + cur = cur['next'] + res.sort(key=lambda x: x[0]) + return res + + +if __name__ == '__main__': + head = None + head = ll_insert(head, 'Pasha', '123-456') + head = ll_insert(head, 'Andrey', '789-012') + head = ll_insert(head, 'Alisa', '345-678') + head = ll_insert(head, 'Anna', '111-222') + print("All:", ll_list_all(head)) + print("Find Pasha:", ll_find(head, 'Pasha')) + head = ll_delete(head, 'Andrey') + print("After delete Andrey:", ll_list_all(head)) \ No newline at end of file