add code
This commit is contained in:
commit
a643c9c885
63
list.py
Normal file
63
list.py
Normal file
@ -0,0 +1,63 @@
|
||||
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()
|
||||
|
||||
|
||||
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:
|
||||
self.head = self.head.next
|
||||
|
||||
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 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
|
72
list_composite_pattern.py
Normal file
72
list_composite_pattern.py
Normal file
@ -0,0 +1,72 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class ListeElement(ABC):
|
||||
@abstractmethod
|
||||
def length(self):
|
||||
pass
|
||||
|
||||
|
||||
class Element(ListeElement):
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.next = None
|
||||
|
||||
def length(self):
|
||||
# Rekursive Methode zur Berechnung der Länge
|
||||
return 1 + self.next.length()
|
||||
|
||||
|
||||
class Abschluss(ListeElement):
|
||||
def length(self):
|
||||
# Ein Abschluss hat keine Länge
|
||||
return 0
|
||||
|
||||
|
||||
class Liste:
|
||||
def __init__(self):
|
||||
self.head = Abschluss() # Der Kopf der Liste ist ein Abschluss
|
||||
|
||||
def insert(self, value):
|
||||
new_element = Element(value)
|
||||
new_element.next = self.head
|
||||
self.head = new_element
|
||||
|
||||
def remove_first(self):
|
||||
if isinstance(self.head, Abschluss):
|
||||
return # Liste ist leer
|
||||
self.head = self.head.next
|
||||
|
||||
def length(self):
|
||||
return self.head.length()
|
||||
|
||||
def show_list(self):
|
||||
current = self.head
|
||||
while not isinstance(current, Abschluss):
|
||||
print(current.data)
|
||||
current = current.next
|
||||
|
||||
def search(self, value):
|
||||
current = self.head
|
||||
while not isinstance(current, Abschluss):
|
||||
if current.data == value:
|
||||
return current
|
||||
current = current.next
|
||||
return None
|
||||
|
||||
def insert_before(self, value, comp_value):
|
||||
new_element = Element(value)
|
||||
if isinstance(self.head, Abschluss):
|
||||
return # Liste ist leer
|
||||
|
||||
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
|
17
main.py
Normal file
17
main.py
Normal file
@ -0,0 +1,17 @@
|
||||
from list_composite_pattern import Liste
|
||||
|
||||
liste = Liste()
|
||||
liste.insert(3)
|
||||
liste.insert(2)
|
||||
liste.insert(1)
|
||||
|
||||
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")
|
||||
|
||||
liste.insert_before(1.5, 2)
|
||||
print("Inhalt der Liste nach Einfügen von 1.5 vor 2:")
|
||||
liste.show_list()
|
Loading…
Reference in New Issue
Block a user