daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package homework.pkg6;
  6.  
  7.  
  8. /**
  9.  *
  10.  * @author Steven
  11.  */
  12. public class PriorityQueue<E> {
  13.     private ArrayQueue<E>[] queues;
  14.     private int highest_queue;
  15.     private int totalSize;
  16.    
  17.     public PriorityQueue(int highest)
  18.     {
  19.         queues = new ArrayQueue[highest + 1];
  20.         for(int i =0; i < (highest+1); i++)
  21.         {
  22.             queues[i] = new ArrayQueue<E>();
  23.         }
  24.  
  25.         //queues = new ArrayQueue<E>(highest+1);
  26.      
  27.         highest_queue = highest;
  28.     }
  29.    
  30.     public void add(E item, int priority)
  31.     {
  32.         if(priority > 0 && priority < highest_queue)
  33.         {
  34.         queues[priority].add(item);
  35.         }
  36.        
  37.         if(priority > highest_queue)
  38.         {
  39.             highest_queue = priority;
  40.             queues[priority].add(item);
  41.         }
  42.         totalSize++;
  43.     }
  44.      
  45.     public E removeQ()
  46.     {
  47.        
  48.         E item = null;
  49.  
  50.         if(!(queues[highest_queue].isEmpty()))
  51.         {
  52.             item = queues[highest_queue].remove();
  53.         }
  54.  
  55.         return(item);
  56.     }
  57.    
  58.     public E peek()
  59.     {
  60.         return ((E) queues[highest_queue]);
  61.        
  62.     }
  63.    
  64.     public int size()
  65.     {
  66.         return totalSize;
  67.     }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top