Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.56 KB | None | 0 0
  1. trait Stack[+T] {
  2.   def isEmpty: Boolean
  3.   def top: T
  4.   def tail: Stack[T]
  5. }
  6.  
  7. case object Empty extends Stack[Nothing] {
  8.   def isEmpty: Boolean = true
  9.   def top: Nothing = throw new NoSuchElementException()
  10.   def tail: Nothing = throw new NoSuchElementException()
  11. }
  12.  
  13. case class Cons[+T](hd: T, tl: Stack[T]) extends Stack[T] {
  14.   def isEmpty: Boolean = false
  15.   def top: T = hd
  16.   def tail: Stack[T] = tl
  17.   def pop[U >:T]: Cons[U] = Cons(tail.top,tail.tail)
  18.   def push[U >: T](t: U): Stack[U] = Cons(t, this)
  19.  override def toString = top + " " +tail.toString
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement