finished C02
This commit is contained in:
parent
55182e5a47
commit
21a3763767
105
list.py
105
list.py
@ -3,13 +3,14 @@ class Element:
|
||||
self.data = data
|
||||
self.next = None
|
||||
|
||||
# Rekursive Methode zur Berechnung der Länge der Liste
|
||||
def length(self):
|
||||
# Rekursive Methode zur Berechnung der Länge
|
||||
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
|
||||
@ -19,8 +20,8 @@ class Element:
|
||||
previous = previous.next
|
||||
current = current.next
|
||||
|
||||
def remove_last(self):
|
||||
# Rekursive Methode zur Entfernung des letzten Elements
|
||||
def remove_last(self):
|
||||
if self.next.next is not None:
|
||||
self.next.remove_last()
|
||||
else:
|
||||
@ -31,29 +32,35 @@ 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()
|
||||
|
||||
def show_list(self):
|
||||
current = self.head
|
||||
while current is not None:
|
||||
print(current.data)
|
||||
current = current.next
|
||||
|
||||
# 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:
|
||||
@ -62,69 +69,32 @@ class Liste:
|
||||
current = current.next
|
||||
return None
|
||||
|
||||
def search_max(self):
|
||||
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)
|
||||
|
||||
# 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
|
||||
|
||||
def remove_last(self):
|
||||
# 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
|
||||
|
||||
def remove_last_2(self):
|
||||
# Iterative Methode zur Entfernung des letzten Elements
|
||||
def remove_last2(self):
|
||||
current = self.head
|
||||
if current is None:
|
||||
return
|
||||
@ -140,6 +110,39 @@ class Liste:
|
||||
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:
|
||||
@ -153,9 +156,3 @@ class Liste:
|
||||
current = current.next
|
||||
new_element.next = current.next
|
||||
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
24
main.py
@ -1,5 +1,7 @@
|
||||
from list import Liste
|
||||
|
||||
# Zum Testen der Methoden der Klasse Liste
|
||||
|
||||
liste = Liste()
|
||||
liste.insert(1)
|
||||
liste.insert(2)
|
||||
@ -8,25 +10,3 @@ liste.insert(3)
|
||||
|
||||
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")
|
||||
|
||||
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())
|
Loading…
Reference in New Issue
Block a user