Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2.  
  3. public class NPQueue<E> {
  4.     // Invariant of the NPQueue class:
  5.     //   1. The number of items in a given priority queue is stored in the
  6.     //      the appropriate slot of the array ManyNodes
  7.     //      manyNodes.
  8.     //   2. The items in the priority queues are stored in a set of linked lists,
  9.     //      with the front of the queue stored at the head node, and the rear of
  10.     //      the queue at the tail node, with the head and tail nodes each being
  11.     //      at the appropriate spot in the front and rear Node arrays with the
  12.     //      position corrosponding to the priority of the objects in the queue.
  13.     //   3. The total number of priorities being represented in the NPQueue object
  14.     //      is stored in the integer NoOfPriority.
  15.     //   4. The total number of nodes in the entire NPQueue object is stored in the
  16.     //      variable counter.
  17.     private int[] manyNodes;
  18.     private int NoOfPriority;
  19.     private int counter;
  20.     private Node<E>[] front;
  21.     private Node<E>[] rear;
  22.  
  23.  
  24.     /**
  25.     * Initialize an empty priority queue.
  26.     * @param - <CODE>highest</CODE>
  27.     *   a value representing the maximum acceptable priority rating
  28.     * <dt><b>Postcondition:</b><dd>
  29.     *   This queue is empty.
  30.     **/  
  31.     public NPQueue(int highest) {
  32.         NoOfPriority = highest;
  33.         @SuppressWarnings({"unchecked"})
  34.         Node<E>[] front = (Node<E>[]) new Node[NoOfPriority];
  35.         Node<E>[] rear = (Node<E>[]) new Node[NoOfPriority];
  36.         manyNodes = new int[NoOfPriority];
  37.         for(int i = 0; i < NoOfPriority; i++) {
  38.             front[i] = null;
  39.             rear[i] = null;
  40.             manyNodes[i] = 0;
  41.         }
  42.         System.out.println(front[2]); // ======THIS WORKS?=======
  43.     }
  44.  
  45.     /**
  46.     * Put a new a new item in this queue.
  47.     * @param <CODE>item</CODE>
  48.     *   the item to be pushed onto this queue
  49.     * @param <CODE>priority</CODE>
  50.     *   the desired priority of the item being added
  51.     * <dt><b>Postcondition:</b><dd>
  52.     *   The item has been pushed onto the appropriate priority queue.
  53.     **/
  54.     public void add(E item, int priority) {
  55.         if (manyNodes[priority] == 0) {  // Insert first item.
  56.             System.out.println(front[2]); // =======THIS DOESNT?========
  57.             // front[priority]; //= new Node<E>(item, null);
  58. //          rear[priority] = front[priority];
  59. //      } else {  // Insert an item that is not the first.
  60. //          rear[priority].addNodeAfter(item);
  61. //          rear[priority] = rear[priority].getLink( );          
  62.         }
  63. //      manyNodes[priority]++;
  64. //      counter++;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement