From 399784d458d515d73dc684af6f88b3f7a7013de4 Mon Sep 17 00:00:00 2001 From: alstergym Date: Fri, 2 Oct 2026 09:00:32 +0200 Subject: [PATCH] len() funktion, fix pop() funktion, assertions in starten.py --- linked-lists/linked_list.py | 27 ++++++++++++++++++++++++--- linked-lists/starten.py | 13 ++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/linked-lists/linked_list.py b/linked-lists/linked_list.py index 693f08a..9539dc5 100644 --- a/linked-lists/linked_list.py +++ b/linked-lists/linked_list.py @@ -55,17 +55,38 @@ class LinkedList: def pop(self) -> int | None: if self.head is None: - self.head = None return None node = self.head + assert node is not None + if node.next is None: + data = node.data + self.head = None + return data + + assert node.next is not None + while True: - if node.next is None: - return node.data + prev_node = node node = node.next + if node.next is None: + data = node.data + prev_node.next = None + return data + + def len(self) -> int: + node = self.head + i = 0 + + while node is not None: + node = node.next + i += 1 + + return i + def print(self): node = self.head while node is not None: diff --git a/linked-lists/starten.py b/linked-lists/starten.py index e69ead2..897aa7f 100644 --- a/linked-lists/starten.py +++ b/linked-lists/starten.py @@ -1,15 +1,26 @@ from linked_list import LinkedList list = LinkedList() +assert list.len() == 0 list.push(123) +assert list.len() == 1 list.push(456) +assert list.len() == 2 list.push(789) -# list.insert(00000, 1) +assert list.len() == 3 +list.insert(-10, 1) list.print() +assert list.len() == 4 +print(list.len()) print(list.pop()) +assert list.len() == 3 print(list.pop()) +assert list.len() == 2 print(list.pop()) +assert list.len() == 1 +print(list.pop()) +assert list.len() == 0 # node3 = Node(333)