Created
September 11, 2012 04:40
-
-
Save wataru420/3696004 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 Queue[T] private ( | |
// 待ち行列の正順リスト | |
private val leading:List[T], | |
// 待ち行列の逆順リスト | |
private val trailing:List[T] | |
){ | |
// 連続パラメータを初期要素とする補助コンストラクタ | |
def this(elems:T*) = this(elems.toList, Nil) | |
// 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