【发布时间】:2016-10-21 11:09:31
【问题描述】:
这是我目前正在使用的功能。它是一个使用节点类的链表。这是implementation of it from my textbook。但是,当我尝试打印时,我得到了这个输出。
main.Node 对象位于 0x000001C1A21E5470> main.Node 对象位于 0x000001C1A21E5438> main.Node 对象位于 0x000001C1A21E54A8> main.Node 对象位于 0x000001C1A21E5400>
我读到使用 __repr__ 可以帮助打印,但是当我实现它时它对我不起作用。
def copyList(self):
links = LinkedList()
current = self.head
while current.getNext() != None:
links.addLast(current)
current= current.getNext()
links.addLast(current.getData())
return links
函数是这样调用的
list2=list1.copyList()
print(list2)
我尝试的 repr 函数
def __repr__(self):
string=''
current = self.head
if current != None:
string += str(current.getData())
current = current.getNext()
counter=2
while current:
string += " "+str(current.getData())
current = current.getNext()
if counter>9:
string=string+'\n'
counter=0
counter=counter+1
return string
【问题讨论】:
-
current.getData()返回什么? -
返回节点的字符串值
标签: python list linked-list nodes