第22行提示'Node ' object has no attribute 'next'
万分感谢
我觉得你的DeleteElementInTail写的不对。我帮你重写了一下,运行没有问题。
代码如下,如果对你有帮助,望采纳
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SingleLinkedList:
def __init__(self):
self.head = Node(None)
def IsEmpty(self):
if self.head.next is None:
print("当前单列表为空")
def InsertElementIn(self, n):
n.next = self.head.next
self.head.next = n
def VisitElement(self, n):
if n:
print(n.data, '->', end='')
def TraverseElement(self):
cNode = self.head
while cNode:
cNode = cNode.next
self.VisitElement(cNode)
def DeleteElementInTail(self):
cNode = self.head
while True:
if cNode.next.next is None:
break
cNode = cNode.next
del cNode.next
cNode.next = None
if __name__ == "__main__":
lst = SingleLinkedList()
lst.IsEmpty()
lst.InsertElementIn(Node(1))
lst.InsertElementIn(Node(2))
lst.InsertElementIn(Node(3))
lst.TraverseElement()
print()
lst.DeleteElementInTail()
lst.TraverseElement()
print()
lst.DeleteElementInTail()
lst.TraverseElement()