So far, we have seen that we can traverse each node of the linked list using a while loop inside the methods. What if we need to iterate from outside using just the linked list object? It is very much possible to achieve this. PHP has a very intuitive iterator interface that allows any external iterators to iterate through an object internally. The Iterator interface provides the following methods:
- Current: Return the current element
- Next: Move forward to the next element
- Key: Return the key of the current element
- Rewind: Rewind the Iterator to the first element
- Valid: Checks whether the current position is valid
We will now implement these methods in our LinkedList class to make our object iterate through the nodes from the object directly. In order to track the current node and the current position within the list during iteration, we will...