由于“类型对象'_Node'没有属性'_element'”,因此无法合并两个排序的单链列表

时间:2019-04-09 10:55:11

标签: python function data-structures merge singly-linked-list

我正在制作自己的merge_list函数。我已经制作了类SList()和类_Node()。但是,当我在merge_list中输入两个排序的列表时,merge_list函数无法比较两个节点的值,并显示type object '_Node' has no attribute '_element',但是_Node类具有_element属性(我也尝试了element,但也不起作用)。我的功能出了什么问题?

这是我的class SList()class _Node()的部分代码以及merge_list的完整代码

class SList:
    class _Node:
        def __init__(self, element, next=None):
            self._element = element
            self._next = next

        def element(self):
            return self._element

        def next(self):
            return self._next

        def set_element(self, element):
            self._element = element

        def set_next(self, next):
            self._next = next

    def __init__(self, head=None):
        self._head = head

    def insert_first(self, element):
        self._head = self._Node(element, self._head)

    def insert_after(self, element, p):
        new_node = self._Node(element, p.next())
        p._next = new_node



。 。 。 和我的merge_list函数

def merge_list(s1, s2):
    s3 = SList()
    s3._Node(None, None)
    prev = s3

    while s1 != None and s2 != None:
        if s1._Node._element<= s2._Node._element: # this line is the problem
            prev.next = s1
            s1 = s1.next
        else:
            prev.next = s2
            s2 = s2.next
        prev = prev.next

    if s1 == None:
        prev.next = s2
    elif s2 == None:
        prev.next = s1

    return s3.next

完成此功能后,我输入了两个这样的排序列表,然后调用merge_list函数

if __name__ == "__main__":
    s1 = SList()
    s1.insert_first(1)
    s1.insert_first(3)
    s1.insert_first(5)
    s1.insert_first(7)
    s1.insert_first(9)
    s1.insert_first(11)
    s1.insert_first(13)
    print(f"s1 :", s1)


    s2 = SList()
    s2.insert_first(2)
    s2.insert_first(4)
    s2.insert_first(6)
    print(f"s2 : ", s2)
    merge_list(s1, s2)

,输出为

Traceback (most recent call last):
s1 : 13 -> 11 -> 9 -> 7 -> 5 -> 3 -> 1 -> None: 7 element(s)
s2 :  6 -> 4 -> 2 -> None: 3 element(s)
  File "", line 334, in <module>
    merge_list(s1, s2)
  File "", line 151, in merge_list
    if s1._Node._element<= s2._Node._element:
AttributeError: type object '_Node' has no attribute '_element'

似乎s1,s2列表已成功创建,但无法比较该节点的值。 merge_list函数有什么问题?

1 个答案:

答案 0 :(得分:0)

让我们一步一步地

代码分析:

class SList:
    class _Node:

注释为什么在SList类中定义Node类?除非您从不希望从Slist外部访问Node(即使如此),否则这不是通常的做法

        def __init__(self, element, next=None):
            self._element = element
            self._next = next

        def element(self):
            return self._element

        def next(self):
            return self._next

        def set_element(self, element):
            self._element = element

        def set_next(self, next):
            self._next = next

注释:初始化/获取器和设置器。到目前为止,一切都有意义。但是,我个人更喜欢@attrs库。但是我们稍后再讲。

    def __init__(self, head=None):
        self._head = head

    def insert_first(self, element):
        self._head = self._Node(element, self._head)

    def insert_after(self, element, p):
        new_node = self._Node(element, p.next())
        p._next = new_node


def merge_list(s1, s2):
    s3 = SList()
    s3._Node(None, None)
    prev = s3

    while s1 != None and s2 != None:
        if s1._Node._element<= s2._Node._element: # this line is the problem

由于注释中提到的原因,这根本行不通:_Node类没有_element属性。实例确实可以。

            prev.next = s1

prevs3SList因此不应该在下一个属性吗?

            s1 = s1.next
        else:
            prev.next = s2
            s2 = s2.next
        prev = prev.next

    if s1 == None:
        prev.next = s2
    elif s2 == None:
        prev.next = s1

    return s3.next

第一次尝试提供帮助(注意:这是基于OP提供的有效解决方案,而不是尝试编写干净的代码。)

class _Node:
    def __init__(self, element, next=None):
        self._element = element
        self._next = next

    @property
    def element(self):
        return self._element
    @property
    def next(self):
        return self._next

    def set_element(self, element):
        self._element = element

    def set_next(self, next):
        self._next = next

class SList:
    def __init__(self, head=None):
        self._head = _Node(head)

    @property
    def head(self):
        return self._head 

    def insert_first(self, element):
        self._head = _Node(element, self._head)

    def insert_after(self, element, p):
        new_node = _Node(element, p.next())
        p._next = new_node

def merge_list(s1, s2):
    s3 = SList()
    prev = s3._head
    head_s1 = s1._head
    head_s2 = s2._head

    while (head_s1._element != None) and (head_s2._element != None):
        if head_s1._element<= head_s2._element: # this line is the problem
            prev.set_next(head_s1)
            head_s1 = head_s1.next
        else:
            prev.set_next(head_s2)
            head_s2 = head_s2.next
        prev = prev.next

    if head_s1 == None:
        prev.set_next(head_s1)
    elif head_s2 == None:
        prev.set_next(head_s2)

    return s3