summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/SingleLinkedList.scala
blob: 67151cb29ef4840ccedd470f6b2ebed0b38e5c38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2006, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection.mutable;


/** This extensible class may be used as a basis for implementing linked
 *  list. Type variable <code>A</code> refers to the element type of the
 *  list, type variable <code>This</code> is used to model self types of
 *  linked lists.
 *
 *  @author  Matthias Zenger
 *  @version 1.0, 08/07/2003
 */
[serializable]
abstract class SingleLinkedList[A, This >: AllRef <: SingleLinkedList[A, This]]
         requires This
         extends AnyRef with Seq[A]
{

    var elem: A = _;

    var next: This = null;

    def length: Int = 1 + (if (next == null) 0 else next.length);

    def append(that: This): Unit =
        if (next == null) { next = that; } else next.append(that);

    def insert(that: This): Unit = if (that != null) {
        that.append(next);
        next = that;
    }

    def apply(n: Int): A = {
        if (n == 0) elem
        else if (next == null) error("unknown element")
        else next.apply(n - 1);
    }

    def get(n: Int): Option[A] = {
        if (n == 0) Some(elem)
        else if (next == null) None
        else next.get(n - 1);
    }

    def elements: Iterator[A] = new Iterator[A] {
        var elems = SingleLinkedList.this;
        def hasNext = (elems != null);
        def next = {
            val res = elems.elem;
            elems = elems.next;
            res;
        }
    }

    override def toList: List[A] =
        if (next == null) (elem :: Nil) else (elem :: next.toList);

}