Hash tables are implemented using buckets containing linked lists. The iteration is simple:
- See if the current node has the next pointer. If so, move on to that.
- If the current node does not have the next pointer, go to the next bucket with node.
- If there is no such node, then you have iterated.
(Find the first node by finding the first bucket with node in it.)
Intuitively, since iterating over the entire hash table using the above O (n) algorithm, it would seem that each “next” operation is an amortized constant time.
source
share