Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to create a queue, then enqueue and dequeue elements in Scala

1 Answer

0 votes
import scala.collection.mutable.Queue

object QueueExample {
  def main(args: Array[String]): Unit = {
    // Create a mutable queue of integers
    val queue = Queue[Int]()

    // Enqueue elements (add elements to the queue)
    queue.enqueue(10)
    queue.enqueue(20, 30, 40) // You can add multiple elements at once
    
    println(s"Queue after enqueueing: $queue") // Output: Queue after enqueueing: Queue(10, 20, 30)

    // Dequeue elements (remove elements from the front of the queue)
    val dequeuedElement = queue.dequeue()
    println(s"Dequeued element: $dequeuedElement") 
    
    println(s"Queue after dequeueing: $queue") 

    // Peek at the front element without removing it
    val frontElement = queue.front
    println(s"Front element: $frontElement")

    // Check if the queue is empty
    println(s"Is the queue empty? ${queue.isEmpty}") 
  }
}


 
 
/*
run:
  
Queue after enqueueing: Queue(10, 20, 30, 40)
Dequeued element: 10
Queue after dequeueing: Queue(20, 30, 40)
Front element: 20
Is the queue empty? false
  
*/

 

 



answered Aug 23, 2025 by avibootz
...