Stack
var fruits = Stack[String]() //empty String Stack
fruits.push("apple")
fruits.push("banana")
fruits.push("coconut", "orange", "pineapple","apple")
//Output
fruits= Stack(apple, pineapple, orange, coconut, banana, apple)
//Take elements off the stack, pop them off:
//fruits stack contains (apple, pineapple, orange, coconut, banana, apple)
val next = fruits.pop
//Output : next: String = apple
//Peek next element of stack without removing it, using
top://fruits contains (apple, pineapple, orange, coconut, banana, apple)
val sTop = fruits.top
//Output : sTop: String = apple
//Size of a Stack
fruits.size //6
//Check whether stack is empty or non-empty
fruits.isEmpty //false
//You can empty a mutable stack with clearfruits.clear
fruits
fruits: scala.collection.mutable.Stack[String] = Stack()
No comments:
Post a Comment