class Element: def __init__(self, data): self.data = data self.next = None # Rekursive Methode zur Berechnung der Länge der Liste def length(self): if self.next is None: return 1 else: return 1 + self.next.length() # Entfernt iterativ das erste gefundene Elemente mit dem Wert value aus der Liste def remove(self,value): previous = self current = self.next while current is not None: if current.data == value: previous.next = current.next previous = previous.next current = current.next # Rekursive Methode zur Entfernung des letzten Elements def remove_last(self): if self.next.next is not None: self.next.remove_last() else: self.next = None class Liste: def __init__(self): self.head = None # Einfügen eines Elements mit Wert value am Anfang der Liste def insert(self, value): new_element = Element(value) new_element.next = self.head self.head = new_element # Entfernt das erste Element aus der Liste def remove_first(self): if self.head is not None: value = self.head.data self.head = self.head.next return value # Methode zur Darstellung der Liste def show_list(self): current = self.head while current is not None: print(current.data) current = current.next print("---") # Bestimmt rekursiv die Länge der Liste def length(self): if self.head is None: return 0 else: return self.head.length() # Sucht nach einem Element mit dem Wert value und gibt den Wert zurück def search(self, value): current = self.head while current is not None: if current.data == value: return current current = current.next return None # Fügt ein Element mit Wert value vor einem Element mit Wert compValue ein 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 # Rekursive Methode zur Entfernung des letzten Elements def remove_last(self): if self.head is not None: if self.head.next is not None: self.head.remove_last() else: self.head = None # Iterative Methode zur Entfernung des letzten Elements def remove_last2(self): current = self.head if current is None: return elif current.next is None: self.head = None return else: previous = self.head current = current.next while current.next is not None: previous = current current = current.next previous.next = None return # Rekursive Methode zur Entfernung eines Elements mit Wert value def remove(self,value): if self.head is None: return if self.head.data == value: self.head = self.head.next else: self.head.remove(value) # Iterative Methode zur Entfernung eines Elements mit Wert value def remove2(self, value): if self.head is None: return if self.head.data == value: self.head = self.head.next return previous = self.head current = previous.next while current is not None: if current.data == value: previous.next = current.next return previous = previous.next current = current.next # Sortiert rekursiv eine Liste nachträglich def sub_sort(self): if self.head is not None: value = self.remove_first() self.sub_sort() self.insert_sorted(value) # Hilfsfunktion zu sub_sort, welches ein neues Element mit Wert value sortiert in eine bestehende Liste einsortiert def insert_sorted(self, value): new_element = Element(value) if self.head is None: self.head = new_element elif self.head.data > value: new_element.next = self.head self.head = new_element else: current = self.head while current.next is not None and current.next.data < value: current = current.next new_element.next = current.next current.next = new_element