Tuesday 8 April 2014

removeFirst : Retrieves and removes the first element of deque

E removeFirst()
Retrieves and removes the last element of this deque.

import java.util.*;

class DequeRemoveFirst{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();
  
  myDeque.offerFirst(10);
  myDeque.offerFirst(20);
  myDeque.offerFirst(30);
  myDeque.offerFirst(40);
  myDeque.offerFirst(50);
  
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);  
  
 }
}

Output
Elements in Deque are
[50, 40, 30, 20, 10]

Removing Head Element 50
Elements in Deque are
[40, 30, 20, 10]

Removing Head Element 40
Elements in Deque are
[30, 20, 10]

Removing Head Element 30
Elements in Deque are
[20, 10]

Removing Head Element 20
Elements in Deque are
[10]

Removing Head Element 10
Elements in Deque are
[]

1. throws NoSuchElementException if this deque is empty
import java.util.*;

class DequeRemoveFirstNoSuch{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedList<Integer> ();
  
  myDeque.offerFirst(10);
  myDeque.offerFirst(20);
  myDeque.offerFirst(30);
  myDeque.offerFirst(40);
  myDeque.offerFirst(50);
  
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque);  
  
  System.out.println("\nRemoving Head Element " + myDeque.removeFirst());
  System.out.println("Elements in Deque are\n" + myDeque); 
 }
}

Output
Elements in Deque are
[50, 40, 30, 20, 10]

Removing Head Element 50
Elements in Deque are
[40, 30, 20, 10]

Removing Head Element 40
Elements in Deque are
[30, 20, 10]

Removing Head Element 30
Elements in Deque are
[20, 10]

Removing Head Element 20
Elements in Deque are
[10]

Removing Head Element 10
Elements in Deque are
[]
Exception in thread "main" java.util.NoSuchElementException
        at java.util.LinkedList.removeFirst(Unknown Source)
        at DequeRemoveFirstNoSuch.main(DequeRemoveFirstNoSuch.java:30)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment