双链表迭代器python
我正在构建一个双链表,我正在努力在PYTHON中构建一个双链表迭代器方法 这是到目前为止我的代码双链表迭代器python,python,Python,我正在构建一个双链表,我正在努力在PYTHON中构建一个双链表迭代器方法 这是到目前为止我的代码 class DoubleListNode: def __init__(self,data): self.data=data self.prev = None self.next= None class ListIterator: def __init__(self): self._current = self.head
class DoubleListNode:
def __init__(self,data):
self.data=data
self.prev = None
self.next= None
class ListIterator:
def __init__(self):
self._current = self.head
def __iter__(self):
return self
def next(self):
if self.size == 0 :
raise StopIteration
else:
item = self._current.data
self._current=self._current.next
return item
class DoublyLinkedList:
def __init__(self):
self.head= None
self.tail= None
self.size = 0
def add(self,data):
newnode= DoubleListNode(data)
self.size+=1
if self.head is None:
self.head = newnode
self.tail = self.head
elif data < self.head.data: # before head
newnode.next = self.head
self.head.prev= newnode
self.head= newnode
elif data > self.tail.data: # at the end
newnode.prev= self.tail
self.tail.next= newnode
self.tail=newnode
else:
curNode = self.head
while curNode is not None and curNode.data < data:
curNode=curNode.next
newnode.next= curNode
newnode.prev=curNode.prev
curNode.prev.next= newnode
curNode.prev=newnode
def remove(self,data):
curNode=self.head
while curNode is not None and curNode.data!= data:
curNode= curNode.next
if curNode is not None:
self.size -= 1
if curNode is self.head:
self.head= curNode.next
else:
curNode.prev.next=curNode.next
if curNode is self.tail:
self.tail=curNode.prev
else:
curNode.next.prev=curNode.prev
class DoubleListNode:
定义初始化(自身,数据):
self.data=data
self.prev=无
self.next=无
类ListIterator:
定义初始化(自):
自。\ U电流=自磁头
定义(自我):
回归自我
def next(自我):
如果self.size==0:
提出停止迭代
其他:
项目=自身。\u当前数据
self.\u current=self.\u current.next
退货项目
类双链接列表:
定义初始化(自):
self.head=无
self.tail=None
self.size=0
def添加(自身、数据):
newnode=DoubleListNode(数据)
自身大小+=1
如果self.head为无:
self.head=newnode
self.tail=self.head
elif数据self.tail.data:#在末尾
newnode.prev=self.tail
self.tail.next=newnode
self.tail=newnode
其他:
curNode=self.head
而curNode不是None且curNode.data<数据:
curNode=curNode.next
newnode.next=curNode
newnode.prev=curNode.prev
curNode.prev.next=newnode
curNode.prev=newnode
def移除(自身、数据):
curNode=self.head
而curNode不是None,curNode.data!=数据:
curNode=curNode.next
如果curNode不是None:
自我尺寸-=1
如果curNode是self.head:
self.head=curNode.next
其他:
curNode.prev.next=curNode.next
如果curNode为self.tail:
self.tail=curNode.prev
其他:
curNode.next.prev=curNode.prev
当我运行一个测试时,它说
TypeError:interaction over non-sequence
。我做错什么了吗?如前所述,代码没有初始化(即未定义self.head)
但总的来说,你在正确的轨道上。请看一个已完成的遍历双链接列表的示例
下面是一个简化的示例:
class Link:
def __init__(self, value, prev=None, next=None):
self.value = value
self.prev = prev
self.next = next
def __iter__(self):
here = self
while here:
yield here.value
here = here.next
def __reversed__(self):
here = self
while here:
yield here.value
here = here.prev
if __name__ == '__main__':
a = Link('raymond')
b = Link('rachel', prev=a); a.next=b
c = Link('matthew', prev=b); b.next=c
print 'Forwards:'
for name in a:
print name
print
print 'Backwards:'
for name in reversed(c):
print name
根据您发布的内容,我可以建议:
class ListIterator:
# other stuff ...
def __iter__(self):
while self._current:
yield self._current.data
self._current = self._current.next
self._current = self.head # Reset the current pointer
您不必实现next()
更新
下面是一个示例用法:
for data in myListIterator:
print data
# Without reset, the second time around won't work:
for data in myListIterator:
print data
我认为有两件重要的事情需要解决 首先,您的
双链接列表
类没有\uu iter\uu
方法。您可能希望创建一个返回listierator
实例的实例。也许您正在尝试手动执行此操作,但这将是正常的方法
其次,您需要修复列表迭代器中的代码才能正常工作。目前,您的\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
下面是一个我认为会起作用的实现:
def ListIterator(object):
def __init__(self, node):
self.current = node
def __iter__(self):
return self
def next(self):
if self.current is None:
raise StopIteration()
result = self.current.data
self.current = self.current.next
return result
class DoublyLinkedList(object):
# all your current stuff, plus:
def __iter__(self):
return ListIterator(self.head)
作为补充说明,在当前代码中定义的类没有基。这在Python3中很好(默认情况下,object
将是基础),但在Python2中,这将导致得到一个“老式”类。旧样式的类已被弃用,您会发现一些语言特性不能与它们一起正常工作(尽管据我所知,迭代中没有涉及任何特性)。另一方面,如果您已经在使用Python3,那么您需要在迭代器类中定义一个\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
方法,而不是next
(不带下划线)。<
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
self.count = 0
def insert(self, val):
newNode = Node(val)
if self.count == 0:
self.head = newNode
self.tail = newNode
else:
self.head.prev = newNode
newNode.next = self.head
self.head = newNode
self.count += 1
def insertToEnd(self, val):
newNode = Node(val)
if self.count == 0:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
newNode.prev = self.tail
self.tail = newNode
self.count += 1
def search(self, val):
p = self.head
while p is not None:
if p.data == val:
return p
p = p.next
def delete(self, val):
curNode = self.head
while curNode != None:
if curNode.data == val:
if curNode.prev != None:
curNode.prev.next = curNode.next
else:
self.head = curNode.next
if curNode.next != None:
curNode.next.prev = curNode.prev
else:
self.tail = curNode.prev
self.count -= 1
curNode = curNode.next
def show(self):
s = ""
p = self.head
while p is not None:
s += str(p.data) + ' ';
p = p.next
print(s + "| count: " + str(self.count))
在\uuuu init\uuuu
中,self.head
来自哪里?如何迭代?如何创建ListIterator
对象?这里肯定出了问题,因为self.\u current=self.head
将在\uuu init\uuu()
中引发和AttributeError
。我在前面实现了一个双链接列表这里是双链接列表类DoublyLinkedList的代码:#init def u init\uuuuu(self):self.head=None-self.tail=None-self.size=0在该类中我有add-remove-len方法请通过发布更完整的代码来帮助其他人,从init开始。消息TypeError:iteration over non-sequence
意味着Python无法为对象找到一个iter\uu方法。还要注意的是,在Python3中,我们使用_next_u而不是next。