The queue.front
function in Scala is used to get the reference of the element at the front of the queue.
Figure 1 shows the visual representation of the queue.front
function.
The following module is required in order to use the function
scala.collection.mutable.Queue
.
queue_name.front;
// where the queue_name is the name of the queue
This function does not require a parameter.
queue.front
is used to get the reference of the element at the queue’s front.
- It does not remove that element from the queue.
- It throws an error if there are no elements in the queue.
The following code shows how to use the queue.front
function.
import scala.collection.mutable.Queueobject Main extends App {var queue = Queue[Int]()queue.enqueue(1);queue.enqueue(3);queue.enqueue(5);queue.enqueue(2);queue.enqueue(0);//Queue = 1->3->5->2->0println("Following are the elements in Queue before using queue.front: " + queue);println("The element in the front of Queue: "+queue.front);println("Following are the elements in Queue after using queue.front: " + queue);}