将节点 class 放入链表 class python

Put node class into linked list class python

第一次学习python , 只是想创建一个简单的链表

这是代码

class node:
    def __init__(self, data = None):
        self.data = data
        self.next = None
        

class linked_list:
    
    def __init__(self):
        self.head = node()
        
    def append(self, data):
        new_node = node(data)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node
    
    def length(self):
        cur = self.head
        total = 0
        while cur.next != None:
            total += 1 
            cur = cur.next
        return total
    
    @property
    def display(self):
        elems = []
        cur_node = self.head
        while cur_node.next != None:
            cur_node = cur_node.next
            elems.append(cur_node.data)
        print(elems)
    
    def get(self, index):
        if index >=  self.length():
            print('index out of range') 
            return None
        cur_idx = 0
        cur_node= self.head
        while True:
            cur_node = cur_node.next
            if cur_idx == index: return cur_node.data
            cur_idx+= 1
    
    def erase(self, index):
        if index >=  self.length():
            print('index out of range') 
            return None
        
        cur_idx = 0
        cur_node = self.head
        while True:
            last_node = cur_node
            cur_node = cur_node.next
            if cur_idx == index:
                last_node.next = cur_node.next
                return 
            cur_idx+= 1

l1 = linked_list()
l1.append(8)
l1.append(7)
l1.append(6)
l1.append(5)


print(l1.get(0))
print(l1.get(1))
print(l1.get(2))
print(l1.get(3))

一切顺利,除了当我尝试将节点 class 作为内部 class 放入链表 class 时,如下所示:

class linked_list:
    class node:
        def __init__(self, data = None):
            self.data = data
            self.next = None
    
    
    def __init__(self):
        self.head = node()
        
    def append(self, data):
        new_node = node(data)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node

......
(the rest are the same as the code above)

我遇到了这个错误:

Traceback (most recent call last):
  File "c:\blahblah\Basic_exercise.py", line 65, in <module>
    l1 = linked_list()
  File "c:\blahblah\Basic_exercise.py", line 11, in __init__
    self.head = node()
NameError: name 'node' is not defined

1.what逻辑我错过了吗?

2.Is 有什么方法可以将节点 class 视为内部 class 而不会出错?

您看到的错误是由于您引用内联函数的方式造成的 class - 这是一个命名空间问题。要消除内部 node class 与其他 node class 的歧义,您需要首先使用外部 class: linked_list.node 来引用它。 示例:

class linked_list:
    class node:
        def __init__(self, data = None):
            self.data = data
            self.next = None
    
    
    def __init__(self):
        self.head = linked_list.node()
        
    def append(self, data):
        new_node = linked_list.node(data)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node

如果要将节点class保留在[​​=13=]class中,则需要从链表class中调用节点class .所以要做到这一点,你必须做 new_node = linked_list.node(data) 。其余都应该一样