Friday 11 April 2014

pollFirst : Retrieves and removes the first element of this deque

E pollFirst()
return the head of this deque, or null if this deque is empty
import java.util.*;

class DequePollFirst{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();
  
  /* Add Elements to the myDeque */
  myDeque.add(10);
  myDeque.add(20);
  myDeque.add(30);
  myDeque.add(40);
  
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
 }
}

Output
Elements in the myDeque are [10, 20, 30, 40]

Removing the First Element Of the Deque 10
Elements in the myDeque are [20, 30, 40]

Removing the First Element Of the Deque 20
Elements in the myDeque are [30, 40]

Removing the First Element Of the Deque 30
Elements in the myDeque are [40]

Removing the First Element Of the Deque 40
Elements in the myDeque are []
1. returns null if this deque is empty.
import java.util.*;

class DequePollFirst{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();
  
  /* Add Elements to the myDeque */
  myDeque.add(10);
  myDeque.add(20);
  myDeque.add(30);
  myDeque.add(40);
  
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
  System.out.println("\nRemoving the First Element Of the Deque " + myDeque.pollFirst());
  System.out.println("Elements in the myDeque are " +myDeque);
  
 }
}
 
Output
Elements in the myDeque are [10, 20, 30, 40]

Removing the First Element Of the Deque 10
Elements in the myDeque are [20, 30, 40]

Removing the First Element Of the Deque 20
Elements in the myDeque are [30, 40]

Removing the First Element Of the Deque 30
Elements in the myDeque are [40]

Removing the First Element Of the Deque 40
Elements in the myDeque are []

Removing the First Element Of the Deque null
Elements in the myDeque are []






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment