From bbcb0c66d7da854430cc1c1c90a8c2c6568df4da Mon Sep 17 00:00:00 2001 From: alstergym Date: Mon, 28 Sep 2026 15:48:34 +0200 Subject: [PATCH] Linked Lists Insert Methode (Noch nicht ausreichend getestet); warum wird 789 mehrfach ausgegeben (unrelated to this commit) --- linked-lists/linked_list.py | 27 +++++++++++++++++++++++++++ linked-lists/starten.py | 1 + 2 files changed, 28 insertions(+) diff --git a/linked-lists/linked_list.py b/linked-lists/linked_list.py index 67afd6f..693f08a 100644 --- a/linked-lists/linked_list.py +++ b/linked-lists/linked_list.py @@ -11,6 +11,33 @@ class LinkedList: def __init__(self): self.head: Node | None = None + def insert(self, data: int, insert_index: int): + if self.head is None: + self.head = Node(data) + return + + node = self.head + assert node is not None + + i = 1 + while True: + ben = Node(37) + + if node.next is None: + node.next = Node(data) + return + if i == insert_index or node.next == ben: + new_node = Node(data) + new_node.next = node.next + node.next = new_node + return + + node = node.next + + i += 1 + print(i, insert_index) + assert i <= insert_index + # Definition der "append" Methode ich mag push und pop lieber >:) def push(self, data: int): if self.head is None: diff --git a/linked-lists/starten.py b/linked-lists/starten.py index 36ba39e..e69ead2 100644 --- a/linked-lists/starten.py +++ b/linked-lists/starten.py @@ -4,6 +4,7 @@ list = LinkedList() list.push(123) list.push(456) list.push(789) +# list.insert(00000, 1) list.print() print(list.pop())