commit a643c9c88570a8d1d546a6fae02d3091e5215c4d Author: mantaru Date: Sun Oct 20 18:27:58 2024 +0200 add code diff --git a/list.py b/list.py new file mode 100644 index 0000000..3750ce9 --- /dev/null +++ b/list.py @@ -0,0 +1,63 @@ +class Element: + def __init__(self, data): + self.data = data + self.next = None + + def length(self): + # Rekursive Methode zur Berechnung der Länge + if self.next is None: + return 1 + else: + return 1 + self.next.length() + + +class Liste: + def __init__(self): + self.head = None + + def insert(self, value): + new_element = Element(value) + new_element.next = self.head + self.head = new_element + + def remove_first(self): + if self.head is not None: + self.head = self.head.next + + def length(self): + if self.head is None: + return 0 + else: + return self.head.length() + + def show_list(self): + current = self.head + while current is not None: + print(current.data) + current = current.next + + def search(self, value): + current = self.head + while current is not None: + if current.data == value: + return current + current = current.next + return None + + def insert_before(self, value, comp_value): + new_element = Element(value) + if self.head is None: + return + + if self.head.data == comp_value: + new_element.next = self.head + self.head = new_element + return + + current = self.head + while current.next is not None and current.next.data != comp_value: + current = current.next + + if current.next is not None: + new_element.next = current.next + current.next = new_element \ No newline at end of file diff --git a/list_composite_pattern.py b/list_composite_pattern.py new file mode 100644 index 0000000..23d5058 --- /dev/null +++ b/list_composite_pattern.py @@ -0,0 +1,72 @@ +from abc import ABC, abstractmethod + +class ListeElement(ABC): + @abstractmethod + def length(self): + pass + + +class Element(ListeElement): + def __init__(self, data): + self.data = data + self.next = None + + def length(self): + # Rekursive Methode zur Berechnung der Länge + return 1 + self.next.length() + + +class Abschluss(ListeElement): + def length(self): + # Ein Abschluss hat keine Länge + return 0 + + +class Liste: + def __init__(self): + self.head = Abschluss() # Der Kopf der Liste ist ein Abschluss + + def insert(self, value): + new_element = Element(value) + new_element.next = self.head + self.head = new_element + + def remove_first(self): + if isinstance(self.head, Abschluss): + return # Liste ist leer + self.head = self.head.next + + def length(self): + return self.head.length() + + def show_list(self): + current = self.head + while not isinstance(current, Abschluss): + print(current.data) + current = current.next + + def search(self, value): + current = self.head + while not isinstance(current, Abschluss): + if current.data == value: + return current + current = current.next + return None + + def insert_before(self, value, comp_value): + new_element = Element(value) + if isinstance(self.head, Abschluss): + return # Liste ist leer + + if self.head.data == comp_value: + new_element.next = self.head + self.head = new_element + return + + current = self.head + while current.next is not None and current.next.data != comp_value: + current = current.next + + if current.next is not None: + new_element.next = current.next + current.next = new_element \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..0a4addd --- /dev/null +++ b/main.py @@ -0,0 +1,17 @@ +from list_composite_pattern import Liste + +liste = Liste() +liste.insert(3) +liste.insert(2) +liste.insert(1) + +print("Inhalt der Liste:") +liste.show_list() + +print("Länge der Liste:", liste.length()) + +print("Suche nach Wert 2:", liste.search(2).data if liste.search(2) else "Nicht gefunden") + +liste.insert_before(1.5, 2) +print("Inhalt der Liste nach Einfügen von 1.5 vor 2:") +liste.show_list() \ No newline at end of file