×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Stack in Scala

By IncludeHelp Last updated : November 14, 2024

A stack is a linear data structure that follows the last-in-first-out (LIFO) principle for adding elements to it.

An example of a stack data structure in real life is a pile of books placed one over another. From this pile, you can remove the book which is placed in the pile at last.

So, in the stack, the addition and removing of elements are done from only one end which is called the top of the stack. In Scala, There can be a mutable stack and immutable stack.

Syntax to create a stack

Mutable stack:

import scala.collection.mutable.Stack
var stack_name = Stack(val1, val2, val3, ...)

Scala program to create a stack

import scala.collection.mutable.Stack

object MyClass {
    def main(args: Array[String]): Unit = {
       val stack1 = Stack(34, 89, 1, 67, 100, 99, 50)
       println("Stack stack1 = " + stack1)
       val stack2 = Stack[String]()
       println("Empty Stack stack2 = " + stack2)
    }
}

Output

Stack stack1 = Stack(34, 89, 1, 67, 100, 99, 50)
Empty Stack stack2 = Stack()

There are some basic operations that can be done on the stack data structure. They are,

  1. Push
  2. Pop

Push operation on stack

The push operation on a stack is simply adding new elements to the stack. The push operation is done using the push() function. The pushed element should be of the same data type.

Syntax

Adding single element to the stack:

stack_name.push(element)

Adding multiple elements to the stack

stack_name.push(element1, element2, element3)

Scala program to push element to the stack

import scala.collection.mutable.Stack

object MyClass {
    def main(args: Array[String]): Unit = {
      val stack1 = Stack[String]()
      
      println("Empty Stack!" + stack1)
      println("Adding single Element to the Stack")
      
      stack1.push("Scala")
      
      println("Stack after push operation : " + stack1)
      println("Adding multiple Elements to the Stack")
      
      stack1.push("Javascript", "Python", "C/C++")
      
      println("Stack after push operation : " + stack1)
    }
}

Output

Empty Stack!Stack()
Adding single Element to the Stack
Stack after push operation : Stack(Scala)
Adding multiple Elements to the Stack
Stack after push operation : Stack(C/C++, Python, Javascript, Scala)

Explanation

The code illustrates the push operation on the stack. Here, we have created an empty stack named stack1 and then push elements to the stack using the push() method. The method can take one or multiple elements and push them to the Stack. When we push multiple elements to the stack. The last parameter to the function will be at the top of the stack.

Pop operation on stack

The pop operation on stack is done to remove the last added element to the stack. The pop (remove) operation, as we know, will be done from the top of the stack using the pop function.

The pop function pops one element from the stack and then returns it to the calling code i.e. the popped value is returned using the pop function.

Syntax

pop

Scala program to pop elements from the stack

import scala.collection.mutable.Stack

object MyClass {
    def main(args: Array[String]): Unit = {
      val langStack = Stack[String]("C/C++", "Scala", "Python", "Javascript")

      println("The Stack : " + langStack)

      println("Popping elements from stack ")
      println("Popped element : "+ langStack.pop)
      println("The Stack after pop operation : " + langStack)
      println("Popped element : "+ langStack.pop)
      println("The Stack after pop operation : " + langStack)
    }
}

Output

The Stack : Stack(C/C++, Scala, Python, Javascript)
Popping elements from stack 
Popped element : C/C++
The Stack after pop operation : Stack(Scala, Python, Javascript)
Popped element : Scala
The Stack after pop operation : Stack(Python, Javascript)

Explanation

In the above code, we have illustrated the pop operation on the stack. Here, we have created a stack named langStack of string type with four elements in it. Then we have used the pop method to pop the element at the top of the stack. We have printed the popped element as the pop method returns the pop element.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.