Advertisement
LoganBlackisle

queue

Jun 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. package prep_28_queue;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5.  
  6. public class Example {
  7. public static void main(String[] args) {
  8. Queue<Integer> q = new LinkedList<>();
  9.  
  10. // Adds elements {0, 1, 2, 3, 4} to queue
  11. for (int i = 0; i < 5; i++)
  12. q.add(i);
  13.  
  14. // Display contents of the queue.
  15. System.out.println("Elements of queue-" + q);
  16.  
  17. // To remove the head of queue.
  18. int removedele = q.remove();
  19.  
  20. System.out.println("removed element-" + removedele);
  21. System.out.println(q);
  22.  
  23. // To view the head of queue
  24. int head = q.peek();
  25.  
  26. System.out.println("head of queue-" + head);
  27.  
  28. // Rest all methods of collection interface,
  29. // Like size and contains can be used with this
  30. // implementation.
  31. int size = q.size();
  32.  
  33. System.out.println("Size of queue-" + size);
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement