Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class Queue[+T] (private val l1:List[T] = Nil, private val l2:List[T] = Nil)
  2. {
  3. class EmptyException(mess:String) extends Exception(mess)
  4.  
  5. def enqueue[S>:T](a:S):Queue[S] =
  6. (this.l1, this.l2) match
  7. {
  8. case (Nil, Nil) => new Queue[S](List(a), Nil)
  9. case (xl,yl) => new Queue[S](xl, a::yl)
  10. }
  11.  
  12. def dequeue():Queue[T] =
  13. (this.l1, this.l2) match
  14. {
  15. case (Nil, Nil) => this
  16. case (List(sth), yl) => new Queue[T](yl.reverse, Nil)
  17. case (h::t, yl) => new Queue[T](t, yl)
  18. case (Nil, h::t) => throw new Exception("To nie jest postać normalna")
  19. }
  20.  
  21. def first():T =
  22. this.l1 match
  23. {
  24. case h::_ => h
  25. case Nil => throw new EmptyException("Kolejka pusta")
  26. }
  27.  
  28. def isEmpty():Boolean =
  29. (l1, l2) == (Nil, Nil)
  30. }
  31.  
  32. var stackOfPoints = new Queue()
  33. var stackOfPixels = new Queue()
  34.  
  35. object Queue
  36. {
  37. def apply[T](xs: T*) = new Queue[T](xs.toList.reverse)
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement