我正在创建一个实现为双向链表的 FIFO,但我不知道为什么会收到递归错误。我已经发布了我的代码和我在下面收到的错误。任何帮助将非常感激!
"""DLList.py"""
class DLList(object):
"""A doubly-linked list
attributes: nl = node list"""
def __init__(self, nl=[]):
self.nl = nl
def __str__(self):
return str(self.nl)
def __len__(self):
return len(self.nl)
def append(self, node):
if not isinstance(node, DLNode):
raise TypeError
try:
node.pn = self.nl[-1]
node.pn.nn = node
self.nl.append(node)
except:
self.nl.append(node)
def pop(self):
rn = self.nl.pop(0)
try:
self.nl[0].pn = None
except:
pass
return rn
class DLNode(object):
"""A node in a doubly-linked list.
attributes: self.pn = previous node, self.data = data, self.nn = next node"""
def __init__(self, data):
self.pn = None
self.data = data
self.nn = None
def __str__(self):
return '[%s, %s, %s]' % (self.pn, self.data, self.nn)
a = DLNode(17)
b = DLNode(15)
c = DLNode(12)
d = DLNode(46)
e = DLNode(46)
ages = DLList()
ages.append(a)
ages.append(b)
ages.append(c)
ages.append(d)
ages.append(e)
print ages.pop()
我收到了这个错误:
File "C:\python\swampy-2.0\DLList.py", line 43, in __str__
return '[%s, %s, %s]' % (self.pn, self.data, self.nn)
RuntimeError: maximum recursion depth exceeded
问题是,我从未打算使用递归,而且我无法弄清楚为什么我进入了递归循环。ages.pop() 旨在返回 DLNode 的实例。