Skip to content

Instantly share code, notes, and snippets.

@melpomene
Created August 3, 2012 01:18
Show Gist options
  • Save melpomene/3243139 to your computer and use it in GitHub Desktop.
Save melpomene/3243139 to your computer and use it in GitHub Desktop.
Learning Scala
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