Sunday, 16 August 2020

Queue in Java | Queue in Collection Framework in Java | Queue Data Structure in Java Example

                   In this post, we will see Queue in Java | Queue in Collection Framework in Java | Queue Data Structure in Java Example



Watch this video to know about Queue in Java | Queue in Collection Framework in Java | Queue Data Structure in Java Example: 



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

Program Code: (QueueProg.java) 


import java.util.*;
public class QueueProg {
      public static void main(String args[]) {
      Queue q = new LinkedList();
      int head;
      System.out.println("Queue: " + q);
      q.add(10);  //offer()
      System.out.println("Queue: " + q);
      q.add(25);
      System.out.println("Queue: " + q);
      q.add(22);
      System.out.println("Queue: " + q);
      q.add(18);
      try {
      System.out.println("Queue: " + q);
      head=(int)q.remove();  //poll()
      System.out.println("Queue: " + q);
      head=(int)q.remove();
      System.out.println("Queue: " + q);
      head=(int)q.remove();
      System.out.println("Queue: " + q);
      head=(int)q.remove(); 
      System.out.println("Queue: " + q);
      head=(int)q.remove();
      }
      catch(Exception e) {
         System.out.println("Queue is Empty");
     }      
              
   }

}
  


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

        

No comments:

Post a comment