Skip to content
Related Articles

Related Articles

Improve Article

Scala Stack reverse() method with example

  • Last Updated : 03 Nov, 2019
Geek Week

In Scala Stack class, the reverse() method is utilized to return a stack with the reverse order.

Method Definition: def reverse: Stack[A]

Return Type: It returns a stack with the reverse order.

Example #1:




// Scala program of reverse() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating stack
        val s1 = Stack(1, 2, 3, 4, 5, 6
          
        // Print the stack 
        println(s1
           
        // Applying reverse method  
        val result = s1.reverse
          
        // Display output 
        print("Reversed stack: " + result) 
           
    
Output:
Stack(1, 2, 3, 4, 5, 6)
Reversed stack: Stack(6, 5, 4, 3, 2, 1)

Example #2:




// Scala program of reverse() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating stack
        val s1 = Stack(5, 2, 13, 7, 1, 3
          
        // Print the stack 
        println(s1
           
        // Applying reverse method  
        val result = s1.reverse
          
        // Display output 
        print("Reversed stack: " + result) 
           
    
Output:
Stack(5, 2, 13, 7, 1, 3)
Reversed stack: Stack(3, 1, 7, 13, 2, 5)



My Personal Notes arrow_drop_up
Recommended Articles
Page :