The Queue interface of the Java collections framework provides the functionality of the queue data structure. It extends the Collection interface.
Classes that Implement Queue:
Since the Queue is an interface, we cannot provide the direct implementation of it. In order to use the functionalities of Queue, we need to use classes that implement it:
In queues, elements are stored and accessed in First In, First Out manner. That is, elements are added from the behind and removed from the front.
In Java, we must import java.util.Queue package in order to use Queue.
Queue<String> names= new ArrayDeque<>();
The above statement creates a Queue of Strings, we can now push and remove Strings from this queue easily.
Methods of Queue:
Complete the code given below.
You need to answer Q queries, in each query you are given two integers x and y:
1 <= Q <= 100
1 <= x <= 3
1 <= y <= 100
First line of input contains a single integer Q.
Next Q lines each contain two integers x and y for that query.
For each query in which x = 2 print its answer in separate lines.
5 2 -1 1 5 3 -1 1 5 2 -1
Query 1: x = 2 so we need to print the front element of the queue , since the queue is empty we will print -1. Query 5: x = 2 Queue contains only element 5 so we will print 5.