A DoubleLinkedList
es al mismo tiempo la propia lista y una lista de nodos, similar a la ::
para un habitual List
. Puede navegar de una celda a la siguiente o a la anterior con next
y prev
, respectivamente, y obtener el valor de una celda con elem
.
scala> val list = collection.mutable.DoubleLinkedList(1,2,3,4,5)
list: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 3, 4, 5)
scala> list.next.next.remove() // list.next.next points on 3rd cell
scala> list
res0: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 4, 5)
Tenga cuidado si se quita la primera celda, ya que necesitará para reasignar su var celebración de la lista a la celda siguiente:
scala> val list = collection.mutable.DoubleLinkedList(1,2,3,4,5)
list: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 3, 4, 5)
scala> list.remove() // remove first item
scala> list // this is now a 'dangling' cell, although it still points to the rest of the list
res6: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 3, 4, 5) // uh? didn't I remove the first cell?
scala> list.next.prev // we can check that it is not pointed back to by its next cell
res7: scala.collection.mutable.DoubleLinkedList[Int] = null
Buena respuesta - un poco de vergüenza los documentos aren tan bueno como StackOverflow! –
¿Hay alguna manera de volver al primero sin llamar a prev muchas veces? – user1377000
No, tienes que seguir llamando a 'prev' hasta que encuentres' null'. –