class Queue { class Node(v: T) { val value: T = v var next: Node? = null } var head: Node? = null var length: Int = 0 private set fun enqueue(v: T) { var newNode = Node(v) if (head == null) { head = newNode } else { var current: Node = head ?: return // can't be null while (current.next != null) { current = current.next ?: return // can't be null } current.next = newNode } length++ } fun dequeue(): T? { val dequeuer = head ?: return null // immutable shadow val retval: T = dequeuer.value head = dequeuer.next length-- return retval } fun isEmpty(): Boolean = length == 0 fun print() { var current: Node? = head print ("[") while (current != null) { print(current.value) current = current.next if (current != null) print(", ") } println("]") } }