Skip to content

Instantly share code, notes, and snippets.

@wataru420
Created September 11, 2012 04:22
Show Gist options
  • Save wataru420/3695939 to your computer and use it in GitHub Desktop.
Save wataru420/3695939 to your computer and use it in GitHub Desktop.
class Queue[T](
// 待ち行列の正順リスト
private val leading:List[T],
// 待ち行列の逆順リスト
private val trailing:List[T]
){
// 2つのリストの同期
private def mirror = {
if(leading.isEmpty) new Queue(trailing.reverse, Nil)
else this
}
// 同期をとった正順リストから先頭要素を取ります
def head = mirror.leading.head
// 同期をとった正順リストからtailを
// 逆順リストはそのままの待ち行列を返します
def tail = {
val q = mirror
new Queue(q.leading.tail, q.trailing)
}
// 逆順リストについて先頭(待ち行列的には末尾)に要素を追加します
def append(x:T) = new Queue(leading, x :: trailing)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment