What is stack.isEmpty in Scala?

Stacks follow the LIFOlast in, first out principle and are available as both mutable and immutable in Scala.

The stack.isEmpty() function in Scala tests whether the stack is empty by returning true if the stack is empty. Otherwise, it returns false.

The image below shows the visual representation of the stack.isEmpty function.

Visual representation of stack.isEmpty function

Syntax

stack_name.isEmpty();
// where the stack_name is the name of the stack

The following module is required in order to use the function: scala.collection.mutable.Stack

Parameters

The stack.isEmpty function does not require any parameters.

Return value

If the stack is empty, the stack.isEmpty function returns true. Otherwise, it returns false.

Code

The following code shows how to use the stack.isEmpty function.

import scala.collection.mutable.Stack
object Main extends App {
//filled stack
var stack_1 = Stack[Int]()
stack_1.push(0);
stack_1.push(2);
stack_1.push(5);
stack_1.push(3);
stack_1.push(1);
println("Stack_1 is empty: " + stack_1.isEmpty);
//empty stack
var stack_2 = Stack[Int]()
println("Stack_2 is empty: " + stack_2.isEmpty);
}

Free Resources