-
Notifications
You must be signed in to change notification settings - Fork 0
/
airlineStack.scala
49 lines (33 loc) · 955 Bytes
/
airlineStack.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
47
48
package airlineFood
import scala.collection.mutable.ListBuffer
object AirlineStack {
private var stack : ListBuffer[(String, Int)] = ListBuffer.empty[(String, Int)]
private var pointer : Int = 0
def addVar (varName : String) =
stack += ((varName, 1))
def pointerDown =
if (pointer > 0)
pointer-=1
def pointerUp =
if (pointer < stack.size-1)
pointer+=1
def pointerToHead =
pointer = stack.size-1
def pointerToVar(varName : String) = {
val index = stack.indexWhere(_._1 == varName)
if (index != -1)
pointer = index
}
def valueOfVar(varName : String) : Int=
(stack.find(_._1 == varName).getOrElse(null,-1))._2
def valueOfVarPointed : Int =
stack.apply(pointer)._2
def changeValOfVarPointed(newValue : Int) = {
stack.update(pointer, (stack.apply(pointer)._1 , newValue))
}
//CAN USE FOR DEBUG
def debugOutput ={
println("Pointer: "+ pointer)
println("Stack: "+ stack)
}
}