Skip to content
Related Articles

Related Articles

Scala Stack +:() method with example
  • Last Updated : 02 Nov, 2019

In Scala, scala.collection.mutable implements Stack data structure. The +: method is similar to ++ method in Stack which gives a copy of the stack with an element prepended. Note that the ending operators are right associative.

Method Definition – def +:(elem: A)

Returns – A new stack consisting of elem followed by all elements of this stack.

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 value 
        val q1 = 1
          
        val q2 = Stack("for", "geeks"
          
        // Applying +:() method 
        val result = q1 +: q2
              
        // Display output 
        print(result) 
          
    
Output:



Stack(1, for, geeks)

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]) 
    
        // Creating a stack 
        val q1 = List(1 )
          
        val q2 = List(11, 12, 13, 14, 15
          
        // Applying ++() method 
        val result = q1.+:(q2
              
        // Display output 
        print(result) 
          
    
Output:
List(List(11, 12, 13, 14, 15), 1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :