From b82a6e6f0de61acd27b5de75b3fbb5660eec6733 Mon Sep 17 00:00:00 2001 From: KislyuninED Date: Thu, 14 May 2026 00:57:12 +0000 Subject: [PATCH] [1] add linked list phonebook implementation --- KislyuninED/docks/data/1-st-exercize/main.py | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 KislyuninED/docks/data/1-st-exercize/main.py diff --git a/KislyuninED/docks/data/1-st-exercize/main.py b/KislyuninED/docks/data/1-st-exercize/main.py new file mode 100644 index 0000000..d86f523 --- /dev/null +++ b/KislyuninED/docks/data/1-st-exercize/main.py @@ -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 \ No newline at end of file