In Scala Stack class
, the push() method is utilized to add an element on the top of the stack.
Method Definition: def push(elem: A): Stack.this.type
Return Type: It adds an element on the top of the stack.
Example #1:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
var s = Stack[Int]()
s.push( 1 )
s.push( 2 )
s.push( 3 )
s.push( 4 )
println(s)
}
}
|
Output:
Stack(4, 3, 2, 1)
Example #2:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
var s = Stack[String]()
s.push( "C++" )
s.push( "Java" )
s.push( "Python" )
s.push( "Scala" )
println(s)
}
}
|
Output:
Stack(Scala, Python, Java, C++)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!