Stack in Scala Last Updated : 25 Apr, 2019 Comments Improve Suggest changes Like Article Like Report A stack is a data structure that follows the last-in, first-out(LIFO) principle. We can add or remove element only from one end called top. Scala has both mutable and immutable versions of a stack. Syntax : import scala.collection.mutable.Stack var s = Stack[type]() // OR var s = Stack(val1, val2, val3, ...) Operations on Stack Once stack has been created we can either push elements to the stack or pop them out of the stack. Push: We can push element of any type to the stack using push() function. All elements must have same data type. Example : scala // Scala program to // push element // to the stack import scala.collection.mutable.Stack // Creating object object GfG { // Main method def main(args:Array[String]) { var s = Stack[Int]() // pushing values // one at a time s.push(5) s.push(1) s.push(2) println("s:" + s) var s2 = Stack[Int]() // pushing multiple values s2.push(5,1,2) println("s2:" + s2) } } Output:s:Stack(2, 1, 5) s2:Stack(2, 1, 5) Pop: We can pop element from top of the stack using pop function. The function returns the same type as that of elements of the stack. Example : scala // Scala program to // pop element from // top of the stack import scala.collection.mutable.Stack // Creating object object GfG { // Main method def main(args:Array[String]) { var s = Stack[Int]() s.push(5) s.push(1) s.push(2) println(s) // pop element from // top of the stack println("Popped:" + s.pop) println("Popped:" + s.pop) println("Popped:" + s.pop) } } Output:Stack(2, 1, 5) Popped:2 Popped:1 Popped:5 Other Functions Other Functions : Let's discuss some more functions with examples. isEmpty: To check whether the stack is empty. Returns true if it is empty. Example : scala // Scala program to // check if the stack // is empty import scala.collection.mutable.Stack // Creating object object GfG { // Main method def main(args:Array[String]) { var s = Stack[Int]() s.push(5) s.push(1) s.push(2) println(s) // pop element from // top of the stack println("Popped:" + s.pop) println("Popped:" + s.pop) println("Empty:" + s.isEmpty) println("Popped:" + s.pop) // all three elements popped println("Empty:" + s.isEmpty) } } Output:Stack(2, 1, 5) Popped:2 Popped:1 Empty:false Popped:5 Empty:true top: Returns the element that is currently at the top of the stack. Example : scala // Scala program to // print top of stack import scala.collection.mutable.Stack // Creating object object GfG { // Main method def main(args:Array[String]) { var s = Stack[Int]() s.push(5) s.push(1) s.push(2) println(s) println("Top: " + s.top) println("Popped:" + s.pop) println("Top: " + s.top) } } Output:Stack(2, 1, 5) Top: 2 Popped:2 Top: 1 size:Returns the number of elements present in the stack. Example : Scala // Scala program to // print size of the stack import scala.collection.mutable.Stack // Creating object object GfG { // Main method def main(args:Array[String]) { var s = Stack[Int]() s.push(5) s.push(1) s.push(2) println(s) println("Size: " + s.size) println("Popped:" + s.pop) println("Size: " + s.size) } } Output:Stack(2, 1, 5) Size: 3 Popped:2 Size: 2 Comment More info M MohammadKhalid Follow Improve Article Tags : Scala Scala scala-collection Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like