finished C02

This commit is contained in:
mantaru 2024-11-03 18:04:58 +01:00
parent 55182e5a47
commit 21a3763767
2 changed files with 55 additions and 78 deletions

109
list.py
View File

@ -3,13 +3,14 @@ class Element:
self.data = data self.data = data
self.next = None self.next = None
# Rekursive Methode zur Berechnung der Länge der Liste
def length(self): def length(self):
# Rekursive Methode zur Berechnung der Länge
if self.next is None: if self.next is None:
return 1 return 1
else: else:
return 1 + self.next.length() return 1 + self.next.length()
# Entfernt iterativ das erste gefundene Elemente mit dem Wert value aus der Liste
def remove(self,value): def remove(self,value):
previous = self previous = self
current = self.next current = self.next
@ -19,8 +20,8 @@ class Element:
previous = previous.next previous = previous.next
current = current.next current = current.next
# Rekursive Methode zur Entfernung des letzten Elements
def remove_last(self): def remove_last(self):
# Rekursive Methode zur Entfernung des letzten Elements
if self.next.next is not None: if self.next.next is not None:
self.next.remove_last() self.next.remove_last()
else: else:
@ -31,29 +32,35 @@ class Liste:
def __init__(self): def __init__(self):
self.head = None self.head = None
# Einfügen eines Elements mit Wert value am Anfang der Liste
def insert(self, value): def insert(self, value):
new_element = Element(value) new_element = Element(value)
new_element.next = self.head new_element.next = self.head
self.head = new_element self.head = new_element
# Entfernt das erste Element aus der Liste
def remove_first(self): def remove_first(self):
if self.head is not None: if self.head is not None:
value = self.head.data value = self.head.data
self.head = self.head.next self.head = self.head.next
return value 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): def length(self):
if self.head is None: if self.head is None:
return 0 return 0
else: else:
return self.head.length() return self.head.length()
def show_list(self): # Sucht nach einem Element mit dem Wert value und gibt den Wert zurück
current = self.head
while current is not None:
print(current.data)
current = current.next
def search(self, value): def search(self, value):
current = self.head current = self.head
while current is not None: while current is not None:
@ -62,69 +69,32 @@ class Liste:
current = current.next current = current.next
return None return None
def search_max(self): # Fügt ein Element mit Wert value vor einem Element mit Wert compValue ein
current = self.head
m = 0
while current is not None:
if current.data >= m:
m = current.data
current = current.next
return m
def remove2(self, value):
# Iterative Lösung der Löschfunktion
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
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)
def insert_before(self, value, comp_value): def insert_before(self, value, comp_value):
new_element = Element(value) new_element = Element(value)
if self.head is None: if self.head is None:
return return
if self.head.data == comp_value: if self.head.data == comp_value:
new_element.next = self.head new_element.next = self.head
self.head = new_element self.head = new_element
return return
current = self.head current = self.head
while current.next is not None and current.next.data != comp_value: while current.next is not None and current.next.data != comp_value:
current = current.next current = current.next
if current.next is not None: if current.next is not None:
new_element.next = current.next new_element.next = current.next
current.next = new_element current.next = new_element
# Rekursive Methode zur Entfernung des letzten Elements
def remove_last(self): def remove_last(self):
# Rekursive Methode zur Entfernung des letzten Elements
if self.head is not None: if self.head is not None:
if self.head.next is not None: if self.head.next is not None:
self.head.remove_last() self.head.remove_last()
else: else:
self.head = None self.head = None
def remove_last_2(self): # Iterative Methode zur Entfernung des letzten Elements
# Iterative Methode zur Entfernung des letzten Elements def remove_last2(self):
current = self.head current = self.head
if current is None: if current is None:
return return
@ -140,6 +110,39 @@ class Liste:
previous.next = None previous.next = None
return 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): def insert_sorted(self, value):
new_element = Element(value) new_element = Element(value)
if self.head is None: if self.head is None:
@ -152,10 +155,4 @@ class Liste:
while current.next is not None and current.next.data < value: while current.next is not None and current.next.data < value:
current = current.next current = current.next
new_element.next = current.next new_element.next = current.next
current.next = new_element current.next = new_element
def sub_sort(self):
if self.head is not None:
value = self.remove_first()
self.sub_sort()
self.insert_sorted(value)

24
main.py
View File

@ -1,5 +1,7 @@
from list import Liste from list import Liste
# Zum Testen der Methoden der Klasse Liste
liste = Liste() liste = Liste()
liste.insert(1) liste.insert(1)
liste.insert(2) liste.insert(2)
@ -8,25 +10,3 @@ liste.insert(3)
print("Inhalt der Liste:") print("Inhalt der Liste:")
liste.show_list() 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")
print("---")
#liste.remove(1)
#liste.show_list()
#liste.insert_before(1, 2)
#print("Inhalt der Liste nach Einfügen von 1.5 vor 2:")
#liste.show_list()
#
# print("---")
# liste.remove_last()
# liste.remove_last()
# liste.show_list()
#liste.insert_sorted(5)
#liste.show_list()
print(liste.search_max())