我得到一个缺少位置参数的错误。(python中的链表)

我得到一个缺少位置参数的错误。(python中的链表),python,data-structures,singly-linked-list,Python,Data Structures,Singly Linked List,代码: 错误: 回溯(最近一次呼叫最后一次): 文件“linkedlist.py”,第65行,在 在_开始处插入_(1) TypeError:insert_at_start()缺少1个必需的位置参数:“new_data”您有缩进问题:insert_at_start不是linkedlist类的方法,因为它是在类作用域之外定义的,因此需要2个参数 请尝试以下方法: class node: def __init__(self,data): self.data=data

代码:

错误: 回溯(最近一次呼叫最后一次): 文件“linkedlist.py”,第65行,在 在_开始处插入_(1)
TypeError:insert_at_start()缺少1个必需的位置参数:“new_data”

您有缩进问题:
insert_at_start
不是
linkedlist
类的方法,因为它是在类作用域之外定义的,因此需要2个参数

请尝试以下方法:

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

class linkedlist:
    def __init__(self):
        self.head=None

def printlist(self):
    temp=self.head
    while(temp):
        print(temp.data,end=" ")
        temp=temp.next

def insert_at_start(self,new_data):
    new_node=node(new_data)
    if self.head==None:
        new_node=self.head
    else:
        new_node.next=self.head
        self.head=new_node

l1=linkedlist()
insert_at_start(1)
insert_at_start(5)
insert_at_start(1)
insert_at_start(7)
insert_at_start(5)

l1.printlist()

首先是缩进问题,这两个函数(
printlist
insert\u at\u start
)应该是方法,所以将它们缩进到
linkedlist
类中。还有两个错误:一个是,你需要这个电话

class linkedlist:
    def __init__(self):
        self.head=None

    def printlist(self):
        temp=self.head
        while(temp):
            print(temp.data,end=" ")
            temp=temp.next

    def insert_at_start(self,new_data):
        new_node=node(new_data)
        if self.head==None:
            new_node=self.head
        else:
            new_node.next=self.head
            self.head=new_node

l1=linkedlist()
l1.insert_at_start(1)
l1.insert_at_start(5)
l1.insert_at_start(1)
l1.insert_at_start(7)
l1.insert_at_start(5)
l1.printlist()
i、 e.实例上的方法调用。另一个是逻辑错误,存在于以下行中:

l1.insert_at_start(..)
.head
还不存在时,您不认为
.head
应该是分配给
新节点的节点吗

还要注意,
l1
可能是有史以来最容易混淆的变量名之一,请更改它;请说出你的班名

if self.head == None:
    new_node = self.head