May 2017
Intermediate to advanced
310 pages
8h 5m
English
If you notice how we traverse our list. That one place where we are still exposed to the node class. We need to use node.data to get the contents of the node and node.next to get the next node. But we mentioned earlier that client code should never need to interact with Node objects. We can achieve this by creating a method that returns a generator. It looks as follows:
def iter(self): current = self.tail while current: val = current.data current = current.next yield val
Now list traversal is much simpler and looks a lot better as well. We can completely ignore the fact that there is anything called a Node outside of the list:
for word in words.iter(): print(word)
Notice that since the iter() method yields the ...