Python -verknüpfte Liste - AttributeError: 'Nicht -Etype' -Objekt hat kein Attribut 'get_data' mit LöschfunktionPython

Python-Programme
Anonymous
 Python -verknüpfte Liste - AttributeError: 'Nicht -Etype' -Objekt hat kein Attribut 'get_data' mit Löschfunktion

Post by Anonymous »

Ich bin neu in Python und habe versucht, einfache Datenstrukturen zu lernen. Ich konnte einige Funktionen für eine verknüpfte Liste zusammenhacken und habe Probleme mit meiner Löschfunktion. Heresliste mit der fraglichen Funktion und dem Testfall:
Klasse Knoten:
def init < /strong> (self, initial_data):
self.data = initial_data
self.next = non < /p>

def get_data(self):
return self.data

def get_next(self):
return self.next

def set_data(self, new_data):
self.data = new_data

def set_next(self, new_next):
self.next = new_next
< /code>

Klasse LinkedList:
def init < /strong> (self):
self.head = Keine < /p>

def __str__(self):
output_string = ''

current = self.head
while current is not None:
output_string += str(current.get_data())
next_node = current.get_next()
#gives the pointer to the next node i.e. the next node is that which is next to the current

if next_node is not None:
output_string += "->"

current = next_node

return output_string
#does not need to be changed for ordered linked list
def is_empty(self):
if self.head is None:
return True
else:
return False
def insert(self, data):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.get_data() > data:
stop = True
else:
previous = current
current = current.get_next()
temp = Node(data)
if previous == None:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)

#does not need to be changed for ordered linked list
def size(self):
current = self.head
count = 0
while current != None:
count += 1
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
stop = False

while current is not None and not found and not stop:
if current.get_data() == item:
found = True

else:
current = current.get_next()
return found

def delete(self, item):
current = self.head
previous = None
found = False

while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()

if previous is None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())

def test_nonexistent():
my_list = LinkedList()
my_list.insert(31)
my_list.insert(77)
my_list.insert(17)
my_list.insert(93)
my_list.insert(26)
my_list.insert(54)
assert my_list.size() == 6
my_list.delete(77)
my_list.delete(1)
assert my_list.size() == 5
< /code>

Ich erhalte die Fehlermeldung < /p>


"AttributeError: 'Nonetype' Objekt hat Kein Attribut 'get_data' mit
Funktion "< /p>
< /blockquote>

Ich glaube T mit einem Wert, der nicht in der Liste ist, aber ich bin verblüfft darüber, wie ich es an diesem Punkt zum Laufen bringen kann. Jede Hilfe wird geschätzt!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post