Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, a Stack
is a last-in-first-out (LIFO) data structure. You can push elements onto the stack and pop them off again. Historically, Scala provided a Stack
in its collections library, but starting from Scala 2.13, it was deprecated in favor of List
which can be used as a stack with ::
for push and tail
for pop operations.
Still, for the sake of understanding, I'll explain the traditional stack operations using both the deprecated Stack
from pre-2.13 and the recommended List
approach for 2.13 and later.
Stack
(pre-2.13):import scala.collection.mutable.Stack val stack = Stack[Int]() // Pushing elements onto the stack stack.push(1) stack.push(2) stack.push(3) // Popping elements off the stack val top1 = stack.pop() // 3 val top2 = stack.pop() // 2 // Peek at the top without popping val peek = stack.top // 1
List
(Scala 2.13 and later):You can easily use a List
as a stack. Pushing onto the stack is done with the ::
operator (or +:
), and popping is done with the tail
method.
var stack = List[Int]() // Pushing elements onto the stack stack = 1 :: stack stack = 2 :: stack stack = 3 :: stack // Popping elements off the stack val top1 = stack.head // 3 stack = stack.tail val top2 = stack.head // 2 stack = stack.tail // Peek at the top without popping val peek = stack.head // 1
Note: While the List
approach uses var
to represent the changing state of the stack, remember that the lists themselves are still immutable. Each push or pop operation creates a new list. If you're doing many operations, this is efficient due to structural sharing.
While you can still use Stack
if you're on a pre-2.13 version of Scala, it's recommended to use the List
approach for newer versions since it provides the functionality needed for stack operations and is a standard part of the Scala collections library.
Scala stack implementation example:
// Mutable Stack implementation import scala.collection.mutable.Stack val mutableStack = Stack[Int]() mutableStack.push(1) mutableStack.push(2) val poppedElement = mutableStack.pop() // Returns 2
How to use Stack in Scala:
// Immutable Stack val immutableStack = scala.collection.immutable.Stack(1, 2, 3) val topElement = immutableStack.top // Returns 1
Immutable stack in Scala:
val immutableStack = scala.collection.immutable.Stack(1, 2, 3) val newStack = immutableStack.push(4)
Mutable stack in Scala:
push
and pop
methods to modify the stack directly.import scala.collection.mutable.Stack val mutableStack = Stack[Int]() mutableStack.push(1) mutableStack.push(2) val poppedElement = mutableStack.pop() // Returns 2
Push and pop operations with Scala Stack:
push
operation adds an element to the top of the stack, while pop
removes and returns the top element.val mutableStack = Stack[Int]() mutableStack.push(1) mutableStack.push(2) val poppedElement = mutableStack.pop() // Returns 2
Stack API in Scala:
push
, pop
, top
, isEmpty
, and more for stack operations.val mutableStack = Stack(1, 2, 3) val topElement = mutableStack.top // Returns 1 val isEmptyStack = mutableStack.isEmpty // Returns false
Working with stacks in functional programming Scala:
val immutableStack = scala.collection.immutable.Stack(1, 2, 3) val newStack = immutableStack.push(4)
Scala List as a stack:
::
operator prepends an element to the list, simulating a push operation.var listStack = List(1, 2, 3) listStack = 0 :: listStack // Simulates push: Adds 0 to the top
Common use cases for Scala Stack data structure:
// Use case: Expression evaluation val expression = "3 * (4 + 2)" val result = evaluateExpression(expression)