首页 > 解决方案 > 不理解单链表实现的“超出递归深度”错误

问题描述

我在 linux 中为我的班级运行此代码时遇到了一些麻烦。本周我们在单链表上,我的老师给我的任务是使用节点来表示多项式并按降序列出它们,我在Node课堂上不断遇到最大递归深度超过错误。

这是代码Node

#!/usr/bin/python
import sys

sys.setrecursionlimit(4500)

"""A model containing Node class"""

class Node(object):
        """A single node in a data structure"""

        def __init__(self, coefficient, exponent):
                self.coefficient=coefficient
                self.exponent=exponent

        @property
        def coefficient(self):
                return self.coefficient

        @coefficient.setter
        def coefficient(self, c):
                self.coefficient=c

        @coefficient.deleter
        def coefficient(self):
                del self.coefficient

        @property
        def exponent(self):
                return self.exponent

        @exponent.setter
        def exponent(self, e):
                self.exponent=e

        @exponent.deleter
        def exponent(self):
                del self.exponent

        @property
        def next(self):
                return self.next

        @next.setter
        def next(self, n):
                self.next=n

        @next.deleter
        def next(self):
                del self.next

        def __eq__(self, other):
                if self.exponent==other.exponent:
                        return True
                else:
                        return False

        def __It__(self, other):
                if self.exponent<other.exponent:
                        return True
                else:
                        return False

        def __str(self):
                if self.coefficient>=0:
                        sign="+"
                else:
                        sign=""
                return sign +str(self.coefficient) + "X^" +str(self.exponent)

这是我的List课程的代码:

#!/usr/bin/python

from NodeModule import Node

class List(Node):
        """Linked list with pre-defined Node class"""

        def __init__(self):
                self.head=None
                self.count=0

        def isEmpty(self):
                return self.count==0

        def getSize(self):
                return self.count

        def insert(self, index, o, p):
                if index<0 or index > self.count:
                        return False
                n=Node(o, p)

                if index==0:
                        n.next=self.head
                        self.head=n
                        self.count+=1
                        return True

                walker=self.head
                for i in range(index-1):
                        walker=walker.next
                n.next=walker.next
                walker.next=n
                self.count+=1
                return True

        def delete(self, index):
                if index < 0 or  index > self.count:
                        return False
                if index==0:
                        self.head=self.head.next
                        self.count-=1
                        return True

                walker=self.head
                for i in range(index-1):
                        walker=walker.next

                walker.next=walker.next.next
                self.count-=1
                return True
        def sort(self):
                temp1=self.head.exponent
                walker=self.head
                j=0
                while j < self.count:
                        for i in self.getsize():
                                walker=walker.next
                                temp2=walker.next.exponent
                                if walker.next.exponent > temp1:
                                        insert(0, walker.next.coefficient, walker.next.exponent)
                                        delete(walker.next)
                                while walker.next is not None:
                                        if walker.next.exponent < walker.next.next.exponent:
                                                insert(self.getsize(), walker.next.next.coefficient, walker.next.next.exponent)
                                                delete(walker.next)
                        j+=1

        def str(self):
                if self.isEmpty():
                        return "\nEnd of Polynomial"

                walker=self.head
                output=[]

                while walker is not None:
                        output.append(str(walker))
                        walker=walker.next
                return " + " .join(output)

这是我用来测试我的代码的内容:

#!/usr/bin/python

from NodeModule import Node
from ListModule import List

def readPoly(message):

        l=List()

        n=input(message)
        for i in range(n):
                c=input("Enter the coefficient of term %d" % i)
                e=input("Enter the exponent of term %d" % i)
                l.insert(0, Node(c,e))
        return l

def main():

        l=readPoly("Enter the number of terms of the polynomial: ")
        print l

        l.sort()
        print l

        l.delete(0)
        print (l)

if __name__=='__main__':
        main()

口译员告诉我,错误就self.coefficient=c在我的Node课堂上。我该如何解决这个问题?

标签: python

解决方案


您不应该有一个名为的变量coefficient和一个同名的属性。

查看您对 setter 的定义:

@coefficient.setter
def coefficient(self, c):
    self.coefficient=c

调用时self.coefficient = c,将再次递归调用相同的设置器。


要解决它,您可以使用下划线重命名变量(并更改代码的所有其他部分):

@coefficient.setter
def coefficient(self, c):
    self._coefficient = c

在进一步阅读中,您似乎可以完全省略设置器,因为它们在您的代码中没有任何用途。您的课程的简化版本Node(具有相同的功能)可能是:

class Node:
    def __init__(self, coefficient, exponent):
        self.coefficient = coefficient
        self.exponent = exponent
        self.next = None

    def __str__(self):
        return '{}{}X^{}'.format(
            '+' if self.coefficient >= 0 else '',
            self.coefficient,
            self.exponent)

推荐阅读