Saturday 12 April 2014

Pop : Pops an element from the Deque

E pop()
Pops an element from the stack represented by this deque. This method is equivalent to removeFirst().

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequePop{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  myDeque.add(10);
  myDeque.add(20);
  myDeque.add(30);
  myDeque.add(40);
  myDeque.add(50);
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
 }
}

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

1. throws NoSuchElementException if this deque is empty

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequePop{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  myDeque.add(10);
  myDeque.add(20);
  myDeque.add(30);
  myDeque.add(40);
  myDeque.add(50);
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
  
  System.out.println("Pop Element " + myDeque.pop());
  System.out.println("Elements in the Deque are "+ myDeque);
 }
}

Output
Elements in the Deque are [10, 20, 30, 40, 50]
Pop Element 10
Elements in the Deque are [20, 30, 40, 50]
Pop Element 20
Elements in the Deque are [30, 40, 50]
Pop Element 30
Elements in the Deque are [40, 50]
Pop Element 40
Elements in the Deque are [50]
Pop Element 50
Elements in the Deque are []
Exception in thread "main" java.util.NoSuchElementException
        at java.util.concurrent.LinkedBlockingDeque.removeFirst(LinkedBlockingDe
que.java:450)
        at java.util.concurrent.LinkedBlockingDeque.pop(LinkedBlockingDeque.java
:773)
        at DequePop.main(DequePop.java:30)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment