Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Stack/Scala/stack-1.scala
Normal file
17
Task/Stack/Scala/stack-1.scala
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class Stack[T] {
|
||||
private var items = List[T]()
|
||||
|
||||
def isEmpty = items.isEmpty
|
||||
|
||||
def peek = items match {
|
||||
case List() => error("Stack empty")
|
||||
case head :: rest => head
|
||||
}
|
||||
|
||||
def pop = items match {
|
||||
case List() => error("Stack empty")
|
||||
case head :: rest => items = rest; head
|
||||
}
|
||||
|
||||
def push(value: T) = items = value +: items
|
||||
}
|
||||
9
Task/Stack/Scala/stack-2.scala
Normal file
9
Task/Stack/Scala/stack-2.scala
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import collection.mutable.{ Stack => Stak }
|
||||
|
||||
class Stack[T] extends Stak[T] {
|
||||
override def pop: T = {
|
||||
if (this.length == 0) error("Can't Pop from an empty Stack.")
|
||||
else super.pop
|
||||
}
|
||||
def peek: T = this.head
|
||||
}
|
||||
13
Task/Stack/Scala/stack-3.scala
Normal file
13
Task/Stack/Scala/stack-3.scala
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
object StackTest extends App {
|
||||
|
||||
val stack = new Stack[String]
|
||||
|
||||
stack.push("Peter Pan")
|
||||
stack.push("Suske & Wiske", "Alice in Wonderland")
|
||||
|
||||
assert(stack.peek == "Alice in Wonderland")
|
||||
assert(stack.pop() == "Alice in Wonderland")
|
||||
assert(stack.pop() == "Suske & Wiske")
|
||||
assert(stack.pop() == "Peter Pan")
|
||||
println("Completed without errors")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue