The queue.add()
function in Java is used to insert an element at the back of the queue.
Figure 1 below shows the visual representation of the queue.add()
function.
The following module is required in order to use the function
java.util.*
.
boolean queue_name.add(element)
queue_name
is the name of the queue.
This function requires an element
as a parameter.
This function inserts the element
sent as a parameter at the back of the queue and returns true
.
If there is no capacity in a queue, then it returns an
IllegalStateException
.
import java.util.*;class JAVA {public static void main( String args[] ) {Queue<Integer> Queue = new LinkedList<Integer>();Queue.add(1);Queue.add(3);Queue.add(5);Queue.add(2);//Queue before adding 0= 1->3->5->2System.out.println("Following are the elements in Queue before 0: " + Queue);System.out.println("0 entered at the back of Queue: "+ Queue.add(0));//Queue after adding 0 = 1->3->5->2->0System.out.println("Following are the elements in Queue after 0: " + Queue);}}