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, ...)
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 program to// push element// to the stackimportscala.collection.mutable.Stack// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){vars=Stack[Int]()// pushing values// one at a times.push(5)s.push(1)s.push(2)println("s:"+ s)vars2=Stack[Int]()// pushing multiple valuess2.push(5,1,2)println("s2:"+ s2)}}chevron_rightfilter_noneOutput:
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 program to// pop element from// top of the stackimportscala.collection.mutable.Stack// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){vars=Stack[Int]()s.push(5)s.push(1)s.push(2)println(s)// pop element from// top of the stackprintln("Popped:"+ s.pop)println("Popped:"+ s.pop)println("Popped:"+ s.pop)}}chevron_rightfilter_noneOutput:
Stack(2, 1, 5) Popped:2 Popped:1 Popped:5
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 program to// check if the stack// is emptyimportscala.collection.mutable.Stack// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){vars=Stack[Int]()s.push(5)s.push(1)s.push(2)println(s)// pop element from// top of the stackprintln("Popped:"+ s.pop)println("Popped:"+ s.pop)println("Empty:"+ s.isEmpty)println("Popped:"+ s.pop)// all three elements poppedprintln("Empty:"+ s.isEmpty)}}chevron_rightfilter_noneOutput:
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 program to// print top of stackimportscala.collection.mutable.Stack// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){vars=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)}}chevron_rightfilter_noneOutput:
Stack(2, 1, 5) Top: 2 Popped:2 Top: 1
- size:Returns the number of elements present in the stack.
Example :// Scala program to// print size of the stackimportscala.collection.mutable.Stack// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){vars=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)}}chevron_rightfilter_noneOutput:
Stack(2, 1, 5) Size: 3 Popped:2 Size: 2
Recommended Posts:
- Scala Stack contains() method with example
- Scala Stack pop() method with example
- Scala Stack :() method with example
- Scala Stack /:() method with example
- Scala Stack :+() method with example
- Scala Stack ++:() method with example
- Scala Stack min() method with example
- Scala Stack max() method with example
- Scala Stack map() method with example
- Scala Stack last() method with example
- Scala Stack last() method with example
- Scala Stack top() method with example
- Scala Stack ++ method with example
- Scala Stack sum() method with example
- Scala Stack take() method with example
- Scala Stack +:() method with example
- Scala Stack toArray() method with example
- Scala Stack splitAt() method with example
- Scala Stack mkString() method with example
- Scala Stack toList() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

