Iterator for list of personal data

Report a typo

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.

Sample Input 1:

Nik 30
Sofi 25
Mike 45

Sample Output 1:

Name Nik age 30
Name Sofi age 25
Name Mike age 45
Write a program in Kotlin
class ListStorageIterator(private val workTeam: ListStorage) : Iterator<StoredNode> {

lateinit var current: StoredNode
private var isAccessed: Boolean = false

override fun hasNext(): Boolean {
// put your code here
}

override fun next(): StoredNode {
// put your code here
}
}

fun main() {
// don't change this code
val listStorage = ListStorage(StoredNode("Nik", 30))
listStorage.add(StoredNode("Sofi", 25))
listStorage.add(StoredNode("Mike", 45))
val iterStorage = listStorage.iterator()
while (iterStorage.hasNext()){
println(iterStorage.next().toNodeString())
}
}
___

Create a free account to access the full topic