【问题标题】:Put node class into linked list class python将节点类放入链表类python
【发布时间】:2021-03-09 07:09:02
【问题描述】:

这是我第一次学习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 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.我在这里错过了什么逻辑?

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

【问题讨论】:

  • 为什么要把node类放到linked_list类里面呢? only 所做的事情是将类对象放在另一个类对象的命名空间中。因此,您必须使用OuterClass.InnerClass 来访问它,就像与类命名空间中的任何其他名称一样。通常没有充分的理由这样做。
  • 我认为节点类与链表密切相关,所以也许把它放进去不是一个坏主意?无论如何,谢谢你指出这一点
  • 这不是 Python 中常见的模式。源代码组织的基本单位是模块。

标签: python class linked-list inner-classes


【解决方案1】:

您看到的错误是由于您引用内部类的方式 - 这是一个命名空间问题。要区分您的内部 node 类与其他 node 类,您需要首先使用外部类来引用它: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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多