Я только начинаю работать со структурами данных здесь. Я пытаюсь реализовать связанный список с python. Но продолжайте получать ошибку! Я буду очень признателен за вашу проницательность. Вот мой код
class Node(object):
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = Node()
def append(self, new_element):
current = self.head
if self.head:
while current.next: #line 14
current = current.next
current.next = new_element
else:
self.head = new_element
def length(self):
count = 0
current = self.head
if self.head:
while current.next:
count += count
current = current.next
return count
else:
return count
def display(self):
elements = []
current = self.head
if self.head:
while current.next:
elements.append(current)
current = current.next
print(elements)
else:
print(elements)
my_list = LinkedList()
my_list.append(1)
my_list.append(2) #line 45
my_list.append(3)
my_list.append(4)
my_list.append(5)
my_list.length()
my_list.display()
Это сообщение об ошибке:
Traceback (most recent call last):
File "C:/Users/Desktop/ds/linkedlist.py", line 45, in <module>
my_list.append(2)
File "C:/Users/Desktop/ds/linkedlist.py", line 14, in append
while current.next is not None:
AttributeError: 'int' object has no attribute 'next'
У меня одна и та же ошибка для всех моих методов, то есть добавления, длины и отображения в LinkedList
класс. Ошибка говорит, что current
является int?