Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. package data_structures_java ;
  2. import java.util.Iterator;
  3. import java.util.PriorityQueue ;
  4. import java.util.* ;
  5. public class Queue_implementation {
  6.  
  7. PriorityQueue<Integer> actual_queue ;
  8.  
  9. public Queue_implementation(){
  10. actual_queue = new PriorityQueue<Integer>() ;
  11.  
  12. }
  13.  
  14. public void add(int num){
  15. actual_queue.add(num) ;
  16. }
  17.  
  18. public int remove(){
  19. return actual_queue.remove() ;
  20. }
  21.  
  22. public int peek(){
  23. if( actual_queue.isEmpty()) return -1 ;
  24. else return actual_queue.peek() ;
  25. }
  26.  
  27. public int element(){
  28. return actual_queue.element() ;
  29. }
  30.  
  31. public void print_queue(){
  32. PriorityQueue<Integer>copy = new PriorityQueue<Integer>();
  33. copy.addAll(actual_queue) ;
  34. Iterator<Integer> through = actual_queue.iterator() ;
  35. while(through.hasNext() ) {
  36. System.out.print(through.next() + " ") ;
  37. }
  38. System.out.println() ;
  39.  
  40. actual_queue.addAll(copy) ;
  41.  
  42. }
  43. public static void main(String[] args) {
  44. Queue_implementation x = new Queue_implementation() ;
  45. x.add(10) ;
  46. x.add(9) ;
  47. x.add(8) ;
  48. x.add(7) ;
  49. x.add(6) ;
  50. x.print_queue() ;
  51. }
  52.  
  53. }
  54.  
  55. Object[] queue_object_array = x.toArray() ;
  56. Arrays.sort(queue_object_array) ;
  57.  
  58. System.out.println(Arrays.toString(priorityQueue.toArray()));
  59.  
  60. Object[] arr = priorityQueue.toArray();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement