Sunday 16 August 2020

Deque in Java | Deque Implementation in Java | Deque Data Structure Java with Example

         
                  In this post, we will see Deque in Java | Deque Implementation in Java | Deque Data Structure Java with Example



Watch this video to know about Deque in Java | Deque Implementation in Java | Deque Data Structure Java with Example:




Watch on YouTube: https://www.youtube.com/watch?v=wNd0Jqsu3go

Program Code: (DequeProg.java) 


import java.util.*;
public class DequeProg {
      public static void main(String args[]) {
      Deque q = new ArrayDeque();
      int head;
      System.out.println("Queue: " + q);
      q.addFirst(10);  //offerFirst()
      System.out.println("Queue: " + q);
      q.addFirst(25);
      System.out.println("Queue: " + q);
      q.addLast(22);  //offerLast()
      System.out.println("Queue: " + q);
      q.addLast(18);
      try{
      System.out.println("Queue: " + q);
      head=(int)q.removeFirst();  //pollFirst()
      System.out.println("Queue: " + q);
      head=(int)q.removeFirst();
      System.out.println("Queue: " + q);
      head=(int)q.removeLast();   //pollLast()
      System.out.println("Queue: " + q);
      head=(int)q.removeLast(); 
      System.out.println("Queue: " + q);
      head=(int)q.removeLast(); 
      } 

     catch(Exception e) {
      System.out.println("Queue is empty");
     }  
   }

}
  


Output:
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac DequeProg.java 
Note: DequeProg.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
parag@parag-Inspiron-N4010:~/Desktop/prog$ java DequeProg
Queue: []
Queue: [10]
Queue: [25, 10]
Queue: [25, 10, 22]
Queue: [25, 10, 22, 18]
Queue: [10, 22, 18]
Queue: [22, 18]
Queue: [22]
Queue: []

Queue is empty

        

No comments:

Post a Comment