add remove, remove_last, sub_sort

This commit is contained in:
mantaru 2024-11-03 17:49:02 +01:00
parent 5a7cc1bc1d
commit 55182e5a47
3 changed files with 52 additions and 17 deletions

27
list.py
View File

@ -62,6 +62,15 @@ class Liste:
current = current.next current = current.next
return None 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): def remove2(self, value):
# Iterative Lösung der Löschfunktion # Iterative Lösung der Löschfunktion
if self.head is None: if self.head is None:
@ -135,19 +144,15 @@ class Liste:
new_element = Element(value) new_element = Element(value)
if self.head is None: if self.head is None:
self.head = new_element self.head = new_element
return elif self.head.data > value:
if self.head.data > value:
new_element.next = self.head new_element.next = self.head
self.head = new_element self.head = new_element
return else:
current = self.head
current = self.head 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
current.next = new_element
new_element.next = current.next
current.next = new_element
def sub_sort(self): def sub_sort(self):
if self.head is not None: if self.head is not None:

26
list_klausur.py Normal file
View File

@ -0,0 +1,26 @@
class Element:
def __init__(self, data):
self.data = data
self.next = None
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(self):
pass
def func(self):
current = self.head
m = 0
while current is not None:
if current.data >= m:
m = current.data
current = current.next
return m

16
main.py
View File

@ -15,14 +15,18 @@ print("Suche nach Wert 2:", liste.search(2).data if liste.search(2) else "Nicht
print("---") print("---")
liste.remove(1) #liste.remove(1)
liste.show_list() #liste.show_list()
# liste.insert_before(4, 2) #liste.insert_before(1, 2)
# print("Inhalt der Liste nach Einfügen von 1.5 vor 2:") #print("Inhalt der Liste nach Einfügen von 1.5 vor 2:")
# liste.show_list() #liste.show_list()
# #
# print("---") # print("---")
# liste.remove_last() # liste.remove_last()
# liste.remove_last() # liste.remove_last()
# liste.show_list() # liste.show_list()
#liste.insert_sorted(5)
#liste.show_list()
print(liste.search_max())