【问题标题】:Why is the print show() showing not defined?为什么 print show() 显示未定义?
【发布时间】:2026-02-19 12:45:01
【问题描述】:

我使用我的 pydroid 创建了一个链表,但 show() 正在输出“nameError: name show not defined”。请回答错误的原因。 show() 是为了帮助我从 add() 中打印出我添加的数据

class Node:
    next=None
    data=None
    
    def __init__(self,data):
        self.data=data
        
        
class LinkedList:
    def __init__(self):
        self.head=None
        
    def Empty(self):
            return self.head==None
        
    def size(self):
        current = self.head
        count=0
        while current:
            count +=1
            current = current.next
        return count
        
"""i want to be able to show data i add to the list wit the add()"""    
    def Show(self):
            n= self.head
            while n:
                print(n.data)
                n = n.next
            else:
                print("empty")
                
#the add()              
    def add(self, data):
        new_node=Node(data)
        new_node.next = self.head
        self.head =new_node
        

b=LinkedList()
#a = Node(10)
#b.head=a
b.add(3)
b.add(3)
b.add(3)
print(b.size())
print(Show())

【问题讨论】:

  • 你忘记b.了吗?
  • 你的意思肯定是:b.Show(),而不是print(b.Show())
  • 是的,对不起,我省略了 b.show
  • 谢谢,没有打印功能。

标签: python class oop linked-list nodes


【解决方案1】:

打印是不必要的,只需使用下面列出的:

b.show()

信用@quamerana

【讨论】: