Look at the code below.
The StoredNode class stores personal data.
class StoredNode(private val name: String, private val age: Int, var next : StoredNode? = null){
fun toNodeString(): String {
return "Name $name age $age"
}
}
The ListStorage class implements the Iterable<StoredNode> interface on the StoredNode object.
class ListStorage (var head: StoredNode, var tail: StoredNode = head) : Iterable<StoredNode> {
fun add(newNode: StoredNode) {
tail.next = newNode
tail = newNode
}
override fun iterator(): Iterator<StoredNode> {
return ListStorageIterator(this)
}
}
In the ListStorageIterator class, which implements the Iterator<StoredNode> interface, implement the hasNext() and next() methods.