2

これは2.8.1のScala標準ライブラリのソースからのものです

  /** Append linked list `that` at current position of this linked list
   *  @return the list after append (this is the list itself if nonempty,
   *  or list `that` if list this is empty. )
   */
  def append(that: This): This = {
  @tailrec
    def loop(x: This) {
      if (x.next.isEmpty) x.next = that
      else loop(x.next)
    }
    if (isEmpty) that
    else { loop(repr); repr }
  }

  /** Insert linked list `that` at current position of this linked list
   *  @note this linked list must not be empty
   */
  def insert(that: This): Unit = {
    require(nonEmpty, "insert into empty list")
    if (that.nonEmpty) {
      next = next.append(that)
    }
  }

この最後の行はすべきではありませんnext = that.append(next)か?(つまり、このリンクリストの残りの部分を、挿入するリストの最後に配置しますか?

そうでない場合は、なぜですか?このコードは現在、挿入しているリストを現在のリストの最後に追加します。つまり、appendと同じです。

4

1 に答える 1

3

既知のバグだと思います。

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> val foo = LinkedList(1, 2, 3, 4)
foo: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4)

scala> foo.next insert LinkedList(5, 6, 7, 8)

scala> foo
res2: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5, 6, 7, 8)

LinkedList(5, 6, 7, 8)「現在の位置」に挿入すると仮定すると、最終的な結果はになりますLinkedList(1, 5, 6, 7, 8, 2, 3, 4)

于 2010-12-15T22:26:11.343 に答える