Queue in Scala
Last Updated :
15 Oct, 2019
A queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and a mutable queue. A mutable queue can be updated or extended in place. It means one can change, add, or remove elements of a queue as a side effect. Immutable queue, by contrast, never change.
In Scala, Queue is implemented as a pair of lists. One is used to insert the elements and second to contain deleted elements. Elements are added to the first list and removed from the second list. The two most basic operations of Queue are Enqueue and Dequeue.
- Enqueue – Adding an element at the end of the queue.
- Dequeue – Deleting an element from the beginning of the queue.
Methods in Queue:
- +=: This method is used to add a single element in the end of the queue.
- ++=: This method is used to Insert more than one the element in the end of the queue.
- clear: Remove all elements from the queue.
- dequeue: Returns the first element in the queue
- enqueue: Adds all the elements to the queue.
- equals: Checks if two queues are structurally identical.
- front: Returns the first element in the queue.
- isEmpty: Check if the queue is empty or not.
Below are simple Scala programs to demonstrate these operations:
Example 1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
var q 1 = Queue( 1 , 2 , 3 , 4 , 5 )
print( "Queue Elements: " )
q 1 .foreach((element : Int) => print(element+ " " ))
var firstElement = q 1 .front
println( "\nFirst element in the queue: " + firstElement)
q 1 .enqueue( 10 )
print( "Queue Elements after enqueue: " )
q 1 .foreach((element : Int) => print(element+ " " ))
var deq = q 1 .dequeue
print( "\nQueue Elements after dequeue: " )
q 1 .foreach((element : Int) => print(element+ " " ))
print( "\nDequeued element: " + deq)
println( "\nQueue is empty: " + q 1 .isEmpty)
}
}
|
Output:
Queue Elements: 1 2 3 4 5
First element in the queue: 1
Queue Elements after enqueue: 1 2 3 4 5 10
Queue Elements after dequeue: 2 3 4 5 10
Dequeued element: 1
Queue is empty: false
Example 2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
var fruits = Queue[String]()
fruits.enqueue( "apple" )
fruits.enqueue( "banana" )
fruits.enqueue( "mango" )
fruits.enqueue( "guava" )
print( "Queue Elements: " )
fruits.foreach((element : String) => print(element+ " " ))
var firstElement = fruits.front
println( "\nFirst element in the queue: " + firstElement)
fruits.enqueue( "pineapple" )
print( "Queue Elements after enqueue: " )
fruits.foreach((element : String) => print(element+ " " ))
var deq = fruits.dequeue
print( "\nQueue Elements after dequeue: " )
fruits.foreach((element : String) => print(element+ " " ))
print( "\nDequeued element: " + deq)
println( "\nclear the queue: " + fruits.clear)
println( "\nqueue is empty: " + fruits.isEmpty)
}
}
|
Output:
Queue Elements: apple banana mango guava
First element in the queue: apple
Queue Elements after enqueue: apple banana mango guava pineapple
Queue Elements after dequeue: banana mango guava pineapple
Dequeued element: apple
clear the queue: ()
queue is empty:true
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...