add remove, remove_last, sub_sort
This commit is contained in:
parent
a643c9c885
commit
5a7cc1bc1d
93
list.py
93
list.py
@ -10,6 +10,22 @@ class Element:
|
||||
else:
|
||||
return 1 + self.next.length()
|
||||
|
||||
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
|
||||
|
||||
def remove_last(self):
|
||||
# Rekursive Methode zur Entfernung des letzten Elements
|
||||
if self.next.next is not None:
|
||||
self.next.remove_last()
|
||||
else:
|
||||
self.next = None
|
||||
|
||||
|
||||
class Liste:
|
||||
def __init__(self):
|
||||
@ -22,7 +38,9 @@ class Liste:
|
||||
|
||||
def remove_first(self):
|
||||
if self.head is not None:
|
||||
value = self.head.data
|
||||
self.head = self.head.next
|
||||
return value
|
||||
|
||||
def length(self):
|
||||
if self.head is None:
|
||||
@ -44,6 +62,32 @@ class Liste:
|
||||
current = current.next
|
||||
return None
|
||||
|
||||
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):
|
||||
new_element = Element(value)
|
||||
if self.head is None:
|
||||
@ -61,3 +105,52 @@ class Liste:
|
||||
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
|
||||
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
|
||||
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
|
||||
|
||||
def insert_sorted(self, value):
|
||||
new_element = Element(value)
|
||||
if self.head is None:
|
||||
self.head = new_element
|
||||
return
|
||||
|
||||
if self.head.data > value:
|
||||
new_element.next = self.head
|
||||
self.head = new_element
|
||||
return
|
||||
|
||||
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
|
||||
|
||||
def sub_sort(self):
|
||||
if self.head is not None:
|
||||
value = self.remove_first()
|
||||
self.sub_sort()
|
||||
self.insert_sorted(value)
|
||||
|
21
main.py
21
main.py
@ -1,9 +1,10 @@
|
||||
from list_composite_pattern import Liste
|
||||
from list import Liste
|
||||
|
||||
liste = Liste()
|
||||
liste.insert(3)
|
||||
liste.insert(2)
|
||||
liste.insert(1)
|
||||
liste.insert(2)
|
||||
liste.insert(4)
|
||||
liste.insert(3)
|
||||
|
||||
print("Inhalt der Liste:")
|
||||
liste.show_list()
|
||||
@ -12,6 +13,16 @@ 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:")
|
||||
print("---")
|
||||
|
||||
liste.remove(1)
|
||||
liste.show_list()
|
||||
|
||||
# liste.insert_before(4, 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()
|
Loading…
Reference in New Issue
Block a user