C02_list/list.py

157 lines
4.2 KiB
Python

class Element:
def __init__(self, data):
self.data = data
self.next = None
def length(self):
# Rekursive Methode zur Berechnung der Länge
if self.next is None:
return 1
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):
self.head = None
def insert(self, value):
new_element = Element(value)
new_element.next = self.head
self.head = new_element
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:
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
def search(self, value):
current = self.head
while current is not None:
if current.data == value:
return current
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:
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
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)