Iterative

Special Stacks

  • reverse stack - use a temp stack to reverse the order of insertion

Iterator in Java

Iterator normally has two main functions hasNext() and next(). We use stack to simulate the steps in a recursion.

hasNext() - check whether next value exists. If so, preprocess the information for next().
next() - check hasNext() first. If true, remove the next element and return it.

Iterator in Python

__iter__() returns the iterator which it normally itself 
__next__() returns the next element and raises StopIteration when exhauses all elements 

iter() is similar to __iter__() and next() is similar to __next__()

Last updated