事实证明是错误的,因为超过了时间限制, 但是我已经提出了StopIteration ...
我认为我在迭代部分做错了什么,但是很难发现错误。测试输出保持运行,甚至打印出None值。它是怎么发生的?
class LinkedListIterator:
def __init__(self, head):
self.__current = head.get_next()
def __iter__(self):
return self
def __next__(self):
if self.__current == None:
raise StopIteration
else:
item = self.__current.get_data()
self.__current = self.__current.get_next()
return item
这些是我用来运行程序的输入:
my_list = LinkedListDLL()
my_list.add_to_head(1)
print("Contents:", end=" ")
for node in my_list:
print(node, end=" ")
print()
答案 0 :(得分:1)
此代码用于在到达列表开头时停止迭代。
if self.__current == None:
raise StopIteration
但是,您用与NodeDLL
不同的None
对象表示头部。
您可以保留对头部的引用,然后对照它进行检查:
class LinkedListIterator:
def __init__(self, head):
self._head = head
self._current = head.get_next()
def __iter__(self):
return self
def __next__(self):
if self._current is self._head:
raise StopIteration
else:
item = self._current.get_data()
self._current = self._current.get_next()
return item
答案 1 :(得分:1)
您要实现的是MutableSequence
的API,并实现了双向链接列表。
要在Python中做到这一点,您应该依靠collections.abc
,它可以指导您完成所有必需方法的实现过程。
通过示例,链接列表实际上是从MutableSequence
继承的类。
from collections.abc import MutableSequence
class LinkedList(MutableSequence):
pass
ll = LinkedList()
在实例化具有一些尚未编写的抽象方法的类时,您将获得一个TypeError
,它将指导您完成需要实现的方法。
TypeError: Can't instantiate abstract class LinkedList with abstract methods __delitem__, __getitem__, __len__, __setitem__, insert
尤其要注意,list
或链接列表不是迭代器,而是可迭代。这意味着__iter__
方法不应返回self
并依赖__next__
,而应在链表的内容上返回一个全新的迭代器。
换句话说,您只能通过 iterator 进行一次迭代,并通过和 iterable 进行多次。
事实证明,我已经完全实现了以这种方式实现的双链表。你可以看看。
from collections.abc import MutableSequence
class LinkedList(MutableSequence):
class _Node:
def __init__(self, value, _next=None, _last=None):
self.value, self._next, self._last = value, _next, _last
def __str__(self):
return f'Node({self.value})'
def __init__(self, iterable=()):
self.start = None
self.last = None
empty = object()
iterable = iter(iterable)
first = next(iterable, empty)
if first is empty:
return
current = self._Node(first)
self.start, self.last = current, current
for value in iterable:
new_node = self._Node(value, _last=self.last)
self.last._next = new_node
self.last = new_node
def __len__(self):
if self.start is None:
return 0
else:
return sum(1 for _ in self)
def __iter_nodes(self):
current = self.start
while current is not None:
yield current
current = current._next
def __reversed_iter_nodes(self):
current = self.last
while current is not None:
yield current
current = current._last
def __iter__(self):
for node in self.__iter_nodes():
yield node.value
def __reversed__(self):
for node in self.__reversed_iter_nodes():
yield node.value
def __get_node(self, index):
if index >= 0:
for item in self.__iter_nodes():
if index == 0:
return item
index -= 1
else:
for item in self.__reversed_iter_nodes():
if index == 0:
return item
index += 1
raise IndexError
def __getitem__(self, index):
if index >= 0:
for item in self:
if index == 0:
return item.value
index -= 1
else:
for item in reversed(self):
if index == 0:
return item.value
index += 1
raise IndexError
def __setitem__(self, key, value):
self[key].value = value
def __delitem__(self, key):
node = self[key]
if node._last:
node._last._next = node._next
if node._next:
node._next._last = node._last
def insert(self, index, value):
if index > len(self):
self.last = self._Node(value, _last=self.last)
else:
where = self.__get_node(index)
_last = where._last
new_node = self._Node(value, _next=where, _last=_last)
if _last:
_last._next = new_node
else:
self.start = new_node
where._last = new_node
ll = LinkedList(range(1, 5))
print(*ll)
print(*reversed(ll))
ll.insert(2, 'foo')
print(*ll)
1 2 3 4
4 3 2 1
1 2 foo 3 4