Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- public class NPQueue<E> {
- // Invariant of the NPQueue class:
- // 1. The number of items in a given priority queue is stored in the
- // the appropriate slot of the array ManyNodes
- // manyNodes.
- // 2. The items in the priority queues are stored in a set of linked lists,
- // with the front of the queue stored at the head node, and the rear of
- // the queue at the tail node, with the head and tail nodes each being
- // at the appropriate spot in the front and rear Node arrays with the
- // position corrosponding to the priority of the objects in the queue.
- // 3. The total number of priorities being represented in the NPQueue object
- // is stored in the integer NoOfPriority.
- // 4. The total number of nodes in the entire NPQueue object is stored in the
- // variable counter.
- private int[] manyNodes;
- private int NoOfPriority;
- private int counter;
- private Node<E>[] front;
- private Node<E>[] rear;
- /**
- * Initialize an empty priority queue.
- * @param - <CODE>highest</CODE>
- * a value representing the maximum acceptable priority rating
- * <dt><b>Postcondition:</b><dd>
- * This queue is empty.
- **/
- public NPQueue(int highest) {
- NoOfPriority = highest;
- @SuppressWarnings({"unchecked"})
- Node<E>[] front = (Node<E>[]) new Node[NoOfPriority];
- Node<E>[] rear = (Node<E>[]) new Node[NoOfPriority];
- manyNodes = new int[NoOfPriority];
- for(int i = 0; i < NoOfPriority; i++) {
- front[i] = null;
- rear[i] = null;
- manyNodes[i] = 0;
- }
- System.out.println(front[2]); // ======THIS WORKS?=======
- }
- /**
- * Put a new a new item in this queue.
- * @param <CODE>item</CODE>
- * the item to be pushed onto this queue
- * @param <CODE>priority</CODE>
- * the desired priority of the item being added
- * <dt><b>Postcondition:</b><dd>
- * The item has been pushed onto the appropriate priority queue.
- **/
- public void add(E item, int priority) {
- if (manyNodes[priority] == 0) { // Insert first item.
- System.out.println(front[2]); // =======THIS DOESNT?========
- // front[priority]; //= new Node<E>(item, null);
- // rear[priority] = front[priority];
- // } else { // Insert an item that is not the first.
- // rear[priority].addNodeAfter(item);
- // rear[priority] = rear[priority].getLink( );
- }
- // manyNodes[priority]++;
- // counter++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement