Scala Stack pop() method with example
Last Updated :
03 Nov, 2019
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:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
var s = Stack( "C++" , "Java" , "Python" , "Scala" )
println(s)
println( "Top of the stack: " + s.top)
val result = s.pop
println( "Popped element: " + result)
}
}
|
Output:
Stack(C++, Java, Python, Scala)
Top of the stack: C++
Popped element: C++
Example #2:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
var s = Stack( 1 , 2 , 3 , 4 )
println(s)
println( "Top of the stack: " + s.top)
val result = s.pop
println( "Popped element: " + result)
}
}
|
Output:
Stack(1, 2, 3, 4)
Top of the stack: 1
Popped element: 1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...