len() funktion, fix pop() funktion, assertions in starten.py

This commit is contained in:
alstergym
2026-10-02 09:00:32 +02:00
parent bbcb0c66d7
commit 399784d458
2 changed files with 36 additions and 4 deletions
+24 -3
View File
@@ -55,17 +55,38 @@ class LinkedList:
def pop(self) -> int | None: def pop(self) -> int | None:
if self.head is None: if self.head is None:
self.head = None
return None return None
node = self.head node = self.head
assert node is not None 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: while True:
if node.next is None: prev_node = node
return node.data
node = node.next 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): def print(self):
node = self.head node = self.head
while node is not None: while node is not None:
+12 -1
View File
@@ -1,15 +1,26 @@
from linked_list import LinkedList from linked_list import LinkedList
list = LinkedList() list = LinkedList()
assert list.len() == 0
list.push(123) list.push(123)
assert list.len() == 1
list.push(456) list.push(456)
assert list.len() == 2
list.push(789) list.push(789)
# list.insert(00000, 1) assert list.len() == 3
list.insert(-10, 1)
list.print() list.print()
assert list.len() == 4
print(list.len())
print(list.pop()) print(list.pop())
assert list.len() == 3
print(list.pop()) print(list.pop())
assert list.len() == 2
print(list.pop()) print(list.pop())
assert list.len() == 1
print(list.pop())
assert list.len() == 0
# node3 = Node(333) # node3 = Node(333)