forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.scala
28 lines (20 loc) · 667 Bytes
/
Stack.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Note that this is just for study purposes since Scala provides a built-in Stack collection:
// https://www.scala-lang.org/api/current/scala/collection/mutable/Stack.html
import scala.collection.mutable.ListBuffer
class Stack[Any] {
private var stack: ListBuffer[Any] = ListBuffer()
def push(value: Any): Unit = stack += value
def pop(): Any = stack.remove(stack.length - 1)
override def toString(): String = stack.mkString(", ")
}
object Main extends App {
val stack = new Stack[Int]
println("Pushing 3 values...")
stack.push(1)
stack.push(2)
stack.push(3)
println(stack)
println("Poping 1 value...")
stack.pop()
println(stack)
}