Связанный список Python – AttributeError: объект «NoneType» не имеет атрибута «get_data» с функцией удаленияPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Связанный список Python – AttributeError: объект «NoneType» не имеет атрибута «get_data» с функцией удаления

Сообщение Anonymous »

Я новичок в Python и пытаюсь изучить простые структуры данных. Мне удалось собрать некоторые функции для связанного списка, и у меня возникли проблемы с функцией удаления. Вот список рассматриваемой функции и тестового примера:
class Node:
def init(self, Initial_data):
self.data = Initial_data
self.next = Нет

Код: Выделить всё

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
class LinkedList:
def init(self):
self.head = None

Код: Выделить всё

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
Я получаю сообщение об ошибке


"AttributeError: объект 'NoneType' имеет нет атрибута 'get_data' с
функцией удаления"


Я считаю, что с функцией удаления что-то не так, поскольку она может' Я не могу обрабатывать значение, которого нет в списке, но я не понимаю, как заставить его работать на данный момент. Любая помощь приветствуется!

Подробнее здесь: https://stackoverflow.com/questions/284 ... te-get-dat
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»