Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

7.7 Linked List Iterators

Suppose we want to provide an iterator for our Bag ADT implemented using a linked list as we did for the one implemented using a Python list. The process would be similar, but our iterator class would have to keep track of the current node in the linked list instead of the current element in the Python list. We define a bag iterator class in the listing below

Program Listing
Program: itbag.py
  1. # Defines a linked list iterator for the Bag ADT.
  2. class BagIterator :
  3.   def __init__( self, listHead ):
  4.     self._curNode = listHead
  5.    
  6.   def __iter__( self ):
  7.     return self
  8.    
  9.   def __next__( self ):
  10.     if self._curNode is None :
  11.       raise StopIteration
  12.     else :
  13.       item = self._curNode.item
  14.       self._curNode = self._curNode.next
  15.       return item

which would be placed within the llistbag module that can be used to iterate over the linked list.

When iterating over a linked list, we need only keep track of the current node being processed and thus we use a single data field _curNode in the iterator. This reference will be advanced through the linked list as the for loop iterates over the nodes. As was done with our Python list-based Bag class, the linked list version must include the __iter__ method, which returns an instance of the BagIterator class.

Figure 7.7.1 illustrates the Bag and BagIterator objects at the beginning of the for loop. The _curNode pointer in the BagIterator object is used just like the curNode pointer we used when directly performing a linked list traversal earlier in the chapter. The only difference is that we don't include a while loop since Python manages the iteration for us as part of the for loop. Note, the iterator objects can be used with any singly linked list configuration to traverse the nodes and return the data contained in each.

Figure 7.7.1: Sample Bag and BagIterator objects at the beginning of the for loop.
Page last modified on August 26, 2023, at 01:19 PM