python 单链接实现 无法遍历

python 单链接实现 无法遍历

如下,建立了 节点链表 的 类,但是无法遍历,如果使用链表类的方法 append 也只能一开始继承链表类的 s 才能使用,而且打印出来的是内存地址,不是 s1 的值 'Tue', 是哪里错了么?
我找了一些文章看,大多都是各种链表的实现方式,但是打印的时候就出错了,是否可以同时附上完整的 链表学习 地址呢?
谢谢回复~

python">class Node(object):

"""docstring for Node"""

def __init__(self, data, next = None):

self.data = data

self.next = next

class LinkedList(object):

"""docstring for LinkedList"""

def __init__(self):

self.head = None

def append(self, data):

'''尾部添加'''

if self.head is None:

self.head = Node(data)

return

else:

value = self.head

while value.next:

value = value.next

value.next = Node(data)

def show(self):

'''遍历'''

value = self.head

while value:

print(value.data)

value = value.next

s = LinkedList()

s.head = Node('Mon')

s1 = Node('Tue')

s2 = Node('Wed')

s.next = s1

s1.next = s2

s.show()

打印结果:

Mon

[Finished in 0.3s]


回答:

s.head.next = s1

以上是 python 单链接实现 无法遍历 的全部内容, 来源链接: utcz.com/p/937672.html

回到顶部