Tuesday 8 April 2014

removeLast : Retrieves and removes the last element of the deque

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


import java.util.*;

class DequeRemoveLast{
 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 Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);  
 }
}

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

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

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

Removing Tail Element 30
Elements in Deque are
[50, 40]

Removing Tail Element 40
Elements in Deque are
[50]

Removing Tail Element 50
Elements in Deque are
[]

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

class DequeRemoveLastNoSuch{
 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 Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque);  
  
  System.out.println("\nRemoving Tail Element " + myDeque.removeLast());
  System.out.println("Elements in Deque are\n" + myDeque); 
 }
}

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

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

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

Removing Tail Element 30
Elements in Deque are
[50, 40]

Removing Tail Element 40
Elements in Deque are
[50]

Removing Tail Element 50
Elements in Deque are
[]
Exception in thread "main" java.util.NoSuchElementException
        at java.util.LinkedList.removeLast(Unknown Source)
        at DequeRemoveLastNoSuch.main(DequeRemoveLastNoSuch.java:30)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment