len() funktion, fix pop() funktion, assertions in starten.py
This commit is contained in:
@@ -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
|
||||
|
||||
while True:
|
||||
if node.next is None:
|
||||
return node.data
|
||||
data = node.data
|
||||
self.head = None
|
||||
return data
|
||||
|
||||
assert node.next is not None
|
||||
|
||||
while True:
|
||||
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:
|
||||
|
||||
+12
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user