[英]Don't understand this “Recursion depth exceeded” error for singly linked list implementation
I'm having a little bit of trouble running this code in linux for my class. 对于我的课程,我在Linux上运行此代码有点麻烦。 This week we're on singly linked list and the assignment my teacher gave me was to use nodes to represent polynomials and list them in descending order I keep coming across a maximum recursion depth exceed error in my
Node
class. 这周我们在单链表上,老师给我的作业是使用节点表示多项式,并以降序排列它们,我一直遇到
Node
类中最大递归深度超过错误的情况。
Here is the code for 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)
Here is the code for my List
class: 这是我的
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)
And here's what I'm using to test my code: 这是我用来测试代码的内容:
#!/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()
The interpreter is telling me that the error is on my self.coefficient=c
line in my Node
class. 解释器告诉我错误在
Node
类的self.coefficient=c
行上。 How can I go about fixing this issue? 我该如何解决这个问题?
You should not have a variable called coefficient
and a property with the same name. 您不应有一个名为
coefficient
的变量和一个具有相同名称的属性。
Look at your definition of the setter: 查看您对二传手的定义:
@coefficient.setter
def coefficient(self, c):
self.coefficient=c
When calling self.coefficient = c
, the same setter will be called again, recursively. 当调用
self.coefficient = c
,将递归地再次调用相同的setter。
To solve it, you could rename your variable with an underscore (and change all the other parts of your code): 要解决此问题,您可以使用下划线将变量重命名(并更改代码的所有其他部分):
@coefficient.setter
def coefficient(self, c):
self._coefficient = c
On further reading , it looks like you could omit the setters altogether, since they do not serve a purpose in your code. 在进一步阅读中,您似乎可以完全省略设置器,因为它们在您的代码中没有用处。 A reduced version of your class
Node
(with the same functionality) could be: 您的
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.