我试图通过数据结构和链表来巩固我的基础知识,但我一直面临这个错误
I'm trying to make my basics strong with data structures and linked lists, I keep facing this error
代码如下:
class Node:
def __init__(self, data, next):
self.data = data
self.next = None
class Linked List:
def __init__(self, data):
self.data = data
self.head = None
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def append(self, data):
self.data = data
new_node = Node(data, next)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node:
last_node = last_node.next
last_node.next = new_node
我这样称呼它:
lst=Linked List()
lst.append("A")
lst.append("B")
lst.print_list()
我得到的错误是:
C:\Users\vaish\PycharmProjects\hello\venv\Scripts\python.exe C:/Users/vaish/PycharmProjects/hello/hackerrank.py
Traceback (most recent call last):
File "C:/Users/vaish/PycharmProjects/hello/hackerrank.py", line 202, in <module>
llist=LinkedList()
TypeError: __init__() missing 1 required positional argument: 'data'
Process finished with exit code 1
有任何更正吗?
你的链表构造函数是一个参数化的 def init(self, data):
因此 python 期望在您使用此 lst=Linked List() 创建链表时的数据值。
只要在那里提供一些数据,它就会起作用。
例如。
lst=链表("A")
您的链表实现有一些错误。我已经评论了您需要更改的代码
class Node:
def __init__(self, data): # do not pass next here
self.data = data
self.next = None # new node should have next=None by default
class Linked_List:
def __init__(self): # no need to pass data here, pass data in append function
self.head = None # self.head is our head of linked list
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def append(self, data):
#self.data = data
new_node = Node(data) # data will be assigned to the newley created node automatically
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next: # you have get the last node
last_node = last_node.next
last_node.next = new_node # this should come outside of the while loop
#set next of last node as newly created node
lst=Linked_List()
lst.append("a")
lst.append("b")
lst.append("c")
lst.print_list()
代码如下:
class Node:
def __init__(self, data, next):
self.data = data
self.next = None
class Linked List:
def __init__(self, data):
self.data = data
self.head = None
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def append(self, data):
self.data = data
new_node = Node(data, next)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node:
last_node = last_node.next
last_node.next = new_node
我这样称呼它:
lst=Linked List()
lst.append("A")
lst.append("B")
lst.print_list()
我得到的错误是:
C:\Users\vaish\PycharmProjects\hello\venv\Scripts\python.exe C:/Users/vaish/PycharmProjects/hello/hackerrank.py
Traceback (most recent call last):
File "C:/Users/vaish/PycharmProjects/hello/hackerrank.py", line 202, in <module>
llist=LinkedList()
TypeError: __init__() missing 1 required positional argument: 'data'
Process finished with exit code 1
有任何更正吗?
你的链表构造函数是一个参数化的 def init(self, data):
因此 python 期望在您使用此 lst=Linked List() 创建链表时的数据值。
只要在那里提供一些数据,它就会起作用。
例如。 lst=链表("A")
您的链表实现有一些错误。我已经评论了您需要更改的代码
class Node:
def __init__(self, data): # do not pass next here
self.data = data
self.next = None # new node should have next=None by default
class Linked_List:
def __init__(self): # no need to pass data here, pass data in append function
self.head = None # self.head is our head of linked list
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def append(self, data):
#self.data = data
new_node = Node(data) # data will be assigned to the newley created node automatically
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next: # you have get the last node
last_node = last_node.next
last_node.next = new_node # this should come outside of the while loop
#set next of last node as newly created node
lst=Linked_List()
lst.append("a")
lst.append("b")
lst.append("c")
lst.print_list()