Python 如何修复显示的链接列表代码:AttributeError:';链表&x27;对象没有属性';头部';

Python 如何修复显示的链接列表代码:AttributeError:';链表&x27;对象没有属性';头部';,python,linked-list,Python,Linked List,我试图按照一个教程创建一个空的链表,但我遇到了一个错误,我不明白。我不熟悉python中的类,所以我不明白当我运行代码时,它说object没有属性头是什么意思 class node: def _init_(self,data=None): self.data=data self.next=None class linked_list: def _init_(self): self.head = node() def ap

我试图按照一个教程创建一个空的链表,但我遇到了一个错误,我不明白。我不熟悉python中的类,所以我不明白当我运行代码时,它说object没有属性头是什么意思

class node:
    def _init_(self,data=None):
        self.data=data
        self.next=None

class linked_list:
    def _init_(self):
        self.head = node()

    def append(self,data):
        new_node = node(data)
        cur = self.head
        while cur.next!=None:
            cur = cur.next
        cur.next = new_node

    def length(self):
        cur = self.head
        total = 0
        while cur.next!=None:
            total+=1
            cur = cur.next
        return total

    def display(self):
        elems = []
        cur_node = self.head
        while cur_node.next!=None:
            cur_node=cur_node.next
            elems.append(cur_node.data)
        print (elems)

my_list = linked_list()

my_list.display()

构造函数的名称不正确:它应该是
\uuuu init\uuuu
(2个下划线),而不是
\uu init\uu

class linked_list:
    def __init__(self):
        self.head = node()

Python认为
\u init\u
只是另一种方法,而不是构造函数。因此,
self.head
的赋值从未发生过。

您的构造函数的名称不正确:它应该是
\uuuuu init\uuuuu>(2个下划线),而不是
\uu init

class linked_list:
    def __init__(self):
        self.head = node()

Python认为
\u init\u
只是另一种方法,而不是构造函数。因此,
self.head
的分配从未发生过。

谢谢,我说我是newThanks,我说我是新来的