-
Notifications
You must be signed in to change notification settings - Fork 258
/
Queue.scala
28 lines (20 loc) · 675 Bytes
/
Queue.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 Queue collection:
// https://www.scala-lang.org/api/current/scala/collection/mutable/Queue.html
import scala.collection.mutable.ListBuffer
class Queue[Any] {
private var queue: ListBuffer[Any] = ListBuffer()
def enqueue(value: Any): Unit = queue += value
def dequeue(): Any = queue.remove(0)
override def toString(): String = queue.mkString(", ")
}
object Main extends App {
val queue = new Queue[Int]
println("Queuing 3 values...")
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
println(queue)
println("Dequeuing 1 value...")
queue.dequeue()
println(queue)
}