Scala Stack :+() method with example
In Scala, scala.collection.mutable implements Stack data structure. The :+ method is used to create a copy of this stack with an element appended.
Method Definition – def :+(elem: A)
Returns – a new stack consisting of all elements of this stack followed by elem.
Example #1:
// Scala program of mutable stack :+() method // Import Stack import scala.collection.mutable. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating a stack val q 2 = List( 11 , 12 , 13 , 14 , 15 ) // Applying :+() method val result = q 2 : + 100 // Display output print(result) } } |
Output:
List(11, 12, 13, 14, 15, 100)
Example #2:
// Scala program of mutable stack /:() // method // Import Stack import scala.collection.mutable. _ // Creating object object GfG { // Main method def main(args : Array[String]) { val st 1 = Stack( 1 , 2 , 3 ) val st 2 = Stack( "Beginner" , "For" ) // Applying :+() method val result 1 = st 2 : + st 1 // Display output print(result 1 ) } } |
Output:
Stack(Beginner, For, Stack(1, 2, 3))