简体   繁体   中英

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. 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.

Here is the code for 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:

#!/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. How can I go about fixing this issue?

You should not have a variable called coefficient and a property with the same name.

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.


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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM