diff --git a/anikinvd/docs/data/1-st-exercise/phonebook.py b/anikinvd/docs/data/1-st-exercise/phonebook.py new file mode 100644 index 0000000..fc7f8aa --- /dev/null +++ b/anikinvd/docs/data/1-st-exercise/phonebook.py @@ -0,0 +1,67 @@ + +def llist_insert(head, name, phone): + current = head + while current is not None: + if current['name'] == name: + current['phone'] = phone + return head + current = current['next'] + + new_node = {'name': name, 'phone': phone, 'next': None} + + if head is None: + return new_node + + current = head + while current['next'] is not None: + current = current['next'] + current['next'] = new_node + return head + + +def llist_find(head, name): + current = head + while current is not None: + if current['name'] == name: + return current['phone'] + current = current['next'] + return None + + +def llist_delete(head, name): + if head is None: + return None + + if head['name'] == name: + return head['next'] + + prev = head + current = head['next'] + while current is not None: + if current['name'] == name: + prev['next'] = current['next'] + return head + prev = current + current = current['next'] + return head + + +def llist_get_all(head): + entries = [] + current = head + while current is not None: + entries.append((current['name'], current['phone'])) + current = current['next'] + entries.sort(key=lambda x: x[0]) + return entries + + +if __name__ == '__main__': + head = None + head = llist_insert(head, "Alice", "111-222") + head = llist_insert(head, "Bob", "333-444") + head = llist_insert(head, "Alice", "555-666") + print(llist_find(head, "Alice")) + print(llist_find(head, "Charlie")) + head = llist_delete(head, "Bob") + print(llist_get_all(head)) \ No newline at end of file