Open In App

Stack in Scala

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.



Other Functions

Other Functions :
Let’s discuss some more functions with examples.


Article Tags :