Skip to content

Instantly share code, notes, and snippets.

@mwhooker
Created September 4, 2009 17:33
Show Gist options
  • Save mwhooker/180999 to your computer and use it in GitHub Desktop.
Save mwhooker/180999 to your computer and use it in GitHub Desktop.
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