user1377000
user1377000

Reputation: 1473

LinkedList: Iterate and remove element

In Scala, while iterating through the elements of a LinkedList, I would like to have some method remove() which removes the current element and (very important) makes the iterator point to the next element (or to the first if the current element is the last one; to null or something if there are no more elements).

Upvotes: 1

Views: 1309

Answers (1)

om-nom-nom
om-nom-nom

Reputation: 62835

Not sure if this what you want but how about:

@annotation.tailrec
def round(xs: Set[(Int, Int)]) = {
    // here you might check if items are exhausted and possibly don't go through recursion deeper


    val suitable = xs.filter {
        case (x, i) => 
            println("Element "+x+" on position "+i)
            x != 0
    }

    // do something with suitable items

    round(xs.diff(suitable)) // next round with items that doesn't succeed in current round
}

val list = List(3,4,5)
round(list.zipWithIndex.toSet)

Upvotes: 0

Related Questions