不久前,我决定学习算法,但我自己做并不容易,所以我正在读古德里奇的《Python中的数据结构和算法》一书,但我还是坚持练习C-7.27Give a recursive implementation of a singly linked list class, such that an instance of a nonempty list stores its first element and a reference to a list of remaining elements. Hint: View the chain of nodes following the head node as themselves forming another list.
我以前用函数做过递归,但在类级别上对我来说有点抽象。
我有这样的基础结构,我不知道如何继续下去。在class SinglyLinkedList:
'''A base class providing a single linked list representation'''
class _Node:
"""non public class for storing a singly linked node"""
__slots__ = '_element', '_next' # streamline memory usage
def __init__(self, element, next_el):
self._element = element
self._next = next_el
def __init__(self):
self._header = self._Node(None, None)
self._header._next = self._header
self._size = 0
def __len__(self):
return self._size
def is_empty(self):
return self._size == 0
据我所知,_Node._next应该携带SinglyLinkedList类,如下所示:
^{pr2}$
但现在要用递归来修饰它,这样我就可以把它想象成一个整体。有人能帮我吗?在
博主寻求帮助解决在使用Python的SinglyLinkedList类中,如何通过递归实现链表结构,包括头节点链接剩余元素的思路。文章将涉及递归概念在类中的应用和链表的递归操作技巧。
191

被折叠的 条评论
为什么被折叠?



