-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueueDemo2.scala
46 lines (33 loc) · 1.08 KB
/
QueueDemo2.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ScalaBasic
// https://www.youtube.com/watch?v=IYpPAwCAZzs&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=128
import scala.collection.mutable
object QueueDemo2 extends App{
// make a queue
val q1 = new mutable.Queue[Int]
q1 ++= List(1,2,3,4,5)
println(q1)
println("===============")
// Delete element (always delete 1st element in queue)
q1.dequeue()
println(q1) // Queue(2, 3, 4, 5)
println("===============")
// Add element (always add element as the final element in queue)
q1.enqueue(9,8,7)
println(q1) // Queue(2, 3, 4, 5, 9, 8, 7)
println("===============")
// Get 1st element in queue
println(q1.head) // not affect queue
println("===============")
// Get last element in queue
println(q1.last) // not affect queue
println("===============")
/*
* Get tail of the queue
* 1) WILL RETURN ALL ELEMENTS EXCEPT 1ST ELEMENT (can use in for loop)
* 2) this OP not affect queue
* 3) return a queue
* 4) this OP can do in looping
*/
println(q1.tail) // not affect queue
println(q1.tail.tail) // not affect queue
}