-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathUsingGenericsForLinkedList.scala
53 lines (43 loc) · 1.27 KB
/
UsingGenericsForLinkedList.scala
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
package com.chapter3.OOP
class UsingGenericsForLinkedList[X] {
private class Node[X](elem: X) {
var next: Node[X] = _
override def toString = elem.toString
}
private var head: Node[X] = _
def add(elem: X) {
val value = new Node(elem)
value.next = head
head = value
}
private def printNodes(value: Node[X]) {
if (value != null) {
println(value)
printNodes(value.next)
}
}
def printAll() { printNodes(head) }
}
object UsingGenericsForLinkedList {
def main(args: Array[String]) {
// To create a list of integers with this class, first create an instance of it, declaring its type as Int:
val ints = new UsingGenericsForLinkedList[Int]()
// Then populate it with Int values:
ints.add(1)
ints.add(2)
ints.add(3)
ints.printAll()
// Because the class uses a generic type, you can also create a LinkedList of String:
val strings = new UsingGenericsForLinkedList[String]()
strings.add("Salman Khan")
strings.add("Aamir Khan")
strings.add("Shah Rukh Khan")
strings.printAll()
// Or any other type such as Double to use:
val doubles = new UsingGenericsForLinkedList[Double]()
doubles.add(10.50)
doubles.add(25.75)
doubles.add(12.90)
doubles.printAll()
}
}