Created
September 4, 2009 17:33
-
-
Save mwhooker/180999 to your computer and use it in GitHub Desktop.
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[T] { | |
private var head : Node = _ | |
def insert(key : String, value : T) { | |
if (head == null) { | |
head = new Node(key, value) | |
} else { | |
var node : Node = head | |
while (node.next != null) { | |
node = node.next | |
} | |
node.next = new Node(key, value) | |
} | |
} | |
class Node(val key : String, val value : T) { | |
private[LinkedList] var next : Node = _; | |
override def toString() = { | |
this.key + " => " + this.value | |
} | |
} | |
def foreach(f : Node => Unit) = { | |
var node : Node = head | |
while (node != null) { | |
f(node) | |
node = node.next | |
} | |
} | |
override def toString() = { | |
var output : String = "" | |
this.foreach(output += _ + "\n") | |
output | |
} | |
} | |
val list = new LinkedList[String] | |
list.insert("key", "value") | |
list.insert("key2", "value".reverse) | |
print(list) | |
// vim: set ts=4 sw=4 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment