Created
August 3, 2012 01:18
-
-
Save melpomene/3243139 to your computer and use it in GitHub Desktop.
Learning Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList(startNode:Node) { | |
var lastNode = startNode | |
def printList() = { | |
startNode.print() | |
} | |
def addNode(node:Node) = { | |
this.lastNode.next = Some(node) | |
node.last = Some(lastNode) | |
this.lastNode = node | |
} | |
} | |
class Node(var data:Option[String], var next:Option[Node], var last:Option[Node]) { | |
def print() : Unit = { | |
println(this.data.getOrElse("- No data -")) | |
next match { | |
case a: Some[_] => a.get.print() | |
case other => println("Error!"); exit | |
} | |
} | |
} | |
object Main{ | |
def main( args:Array[String] ) = { | |
println("Running!") | |
val n1 = new Node(Some("data"), None, None) | |
val ll = new LinkedList(n1) | |
val n2 = new Node(Some("data2"), None, None) | |
val n3 = new Node(None, None, None) | |
ll.addNode(n2) | |
ll.addNode(n3) | |
ll.printList() | |
println("Finished") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment