f*******5 发帖数: 52 | 2 如果用python的话stack里不用放当前下标。当栈顶是数组的话就弹出数组, 然后逆序
入栈。以下是python代码,假定只有list 和 basic type 两种类型
class ListIter(object):
def __init__(self, lst):
self.stack = [lst]
def next(self):
if len(self.stack) == 0:
raise StopIteration
while isinstance(self.stack[-1],list):
l = self.stack.pop()
self.stack.extend([i for i in reversed(l) if not isinstance(i,
list) or len(i)>0])
if len(self.stack) == 0:
raise StopIteration
... 阅读全帖 |
|