Monday 12 May 2014

LinkedList : poll : Retrieves and Removes the Head of the List

public E poll()
Retrieves and removes the head (first element) of this list. Returns null if the list is empty.

import java.util.*;

class LinkedListPoll{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Head Element of myList is ");
  System.out.println(myList.poll());
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 40]
Head Element of myList is 10
Elements in myList are
[20, 30, 40]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment