Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue[+T] (private val l1:List[T] = Nil, private val l2:List[T] = Nil)
- {
- class EmptyException(mess:String) extends Exception(mess)
- def enqueue[S>:T](a:S):Queue[S] =
- (this.l1, this.l2) match
- {
- case (Nil, Nil) => new Queue[S](List(a), Nil)
- case (xl,yl) => new Queue[S](xl, a::yl)
- }
- def dequeue():Queue[T] =
- (this.l1, this.l2) match
- {
- case (Nil, Nil) => this
- case (List(sth), yl) => new Queue[T](yl.reverse, Nil)
- case (h::t, yl) => new Queue[T](t, yl)
- case (Nil, h::t) => throw new Exception("To nie jest postać normalna")
- }
- def first():T =
- this.l1 match
- {
- case h::_ => h
- case Nil => throw new EmptyException("Kolejka pusta")
- }
- def isEmpty():Boolean =
- (l1, l2) == (Nil, Nil)
- }
- var stackOfPoints = new Queue()
- var stackOfPixels = new Queue()
- object Queue
- {
- def apply[T](xs: T*) = new Queue[T](xs.toList.reverse)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement