Skip to content
Related Articles
Open in App
Not now

Related Articles

Scala Stack pop() method with example

Improve Article
Save Article
  • Last Updated : 03 Nov, 2019
Improve Article
Save Article

In Scala Stack class, the pop() method is utilized to remove and return the element at the top of the stack.

Method Definition: def pop(): A

Return Type: It removes and returns the element at the top of the stack.

Example #1:




// Scala program of pop() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
      
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a stack
        var s = Stack("C++", "Java", "Python", "Scala"
          
        // Print the stack
        println(s)
          
        // Print the top of the stack
        println("Top of the stack: " + s.top)
  
        // Applying pop method    
        val result = s.pop
          
        // Print the popped element
        println("Popped element: " + result) 
  
    

Output:

Stack(C++, Java, Python, Scala)
Top of the stack: C++
Popped element: C++

Example #2:




// Scala program of pop() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
      
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a stack
        var s = Stack(1, 2, 3, 4
          
        // Print the stack
        println(s)
          
        // Print the top of the stack
        println("Top of the stack: " + s.top)
  
        // Applying pop method    
        val result = s.pop
          
        // Print the popped element
        println("Popped element: " + result) 
  
    

Output:

Stack(1, 2, 3, 4)
Top of the stack: 1
Popped element: 1

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!