So I'm been trying to figure out what I'm doing wrong with this for hours now and I'm just completely stuck. For this assigment we need to create a Linked List Priority Queue. What's making it difficult for me is that we need to have an array of all the first nodes from the queue. So for example, position 0 in the array should be the first node of the first entry for priority 1. Everything I've been trying is losing what I've previously added to the queue.
Here's some of the code I have so far:
public class LinkedPriorityQueue<T extends Comparable<? super T>> implements PriorityQueueInterface<T> { private Node[] firstNode; // reference to first node of chain and the front // of the priority queue, which has the // highest priority private int length; // number of entries in chain public LinkedPriorityQueue(int max) { firstNode = (Node[])new LinkedPriorityQueue.Node[max]; for (int index = 0; index < max; index ++) { firstNode[index] = new Node(null); } length = 0; } // end default constructor public void add(T newEntry, int priority) { Node newNode = new Node(newEntry, null); Node nodeBefore = getNodeBefore(newEntry, priority); if(nodeBefore == null) { newNode.setNextNode(firstNode[priority]); firstNode[priority] = newNode; } else // add after nodeBefore { Node nodeAfter = nodeBefore.getNextNode(); newNode.setNextNode(nodeAfter); nodeBefore.setNextNode(newNode); } // end if length++; //display(); } // end add private Node getNodeBefore(T anEntry, int priority) { Node currentNode = firstNode[priority]; Node nodeBefore = null; while ((currentNode.getData() != null)) { nodeBefore = currentNode; currentNode = currentNode.getNextNode(); } // end while return nodeBefore; } // end getNodeBefore
Most of this was given to us from our textbook, we're just supposed to modify it to handle an array of first nodes. I think my issue is that I'm not linking the nodes properly when I add a new entry but I can't figure out how to fix it.
Well I'd really appreciate any help you can offer me, and if there's more info you need from me just ask, and sorry for posting so much, I just wanted to make sure you had an enough info to help.