Sunday 6 April 2014

poll : Retrieve and Removes the head Of the Queue

E poll()
Retrieves and removes the head of this queue. return the head of this queue, or null if this queue is empty.

import java.util.*;
class QueuePoll{
 public static void main(String args[]){
  Queue<Integer> myQueue = new PriorityQueue<> ();
  
  /* Add Elements to myQueue */
  System.out.println("Is 10 Added to the Queue " + myQueue.offer(10));
  System.out.println("Is 20 Added to the Queue " + myQueue.offer(20));
  System.out.println("Is 30 Added to the Queue " + myQueue.offer(30));
  System.out.println("Is 40 Added to the Queue " + myQueue.offer(40));
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.poll() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.poll() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Removing the Head Of the Queue " +myQueue.poll());
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.poll() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.poll() + " removed from the Queue "); 
 }
}

Output
Is 10 Added to the Queue true
Is 20 Added to the Queue true
Is 30 Added to the Queue true
Is 40 Added to the Queue true

Elements in myQueue are
[10, 20, 30, 40]
Head Element 10 removed from the Queue

Elements in myQueue are
[20, 40, 30]
Head Element 20 removed from the Queue

Elements in myQueue are
[30, 40]
Removing the Head Of the Queue 30

Elements in myQueue are
[40]
Head Element 40 removed from the Queue

Elements in myQueue are
[]
Head Element null removed from the Queue


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment