Question: -Need help with add method in LinkedPriorityQueue I really dont want some copy and pasted code. An in-depth explanation of what I am doing wrong

-Need help with add method in LinkedPriorityQueue

I really dont want some copy and pasted code. An in-depth explanation of what I am doing wrong is all I really am looking for.

my code for my LinkedPriorityQueue class will be provided at the end.

the addNodeAfter(item) method returns the link of the newly added node

***************************

Mainly just want help with the logic of my addMethod

- Output results for empty queue and highestpriority queue provide the correct output. Everything complies but I am having issues with any priority levels lower than highest.

requirements:

The behavior of add and remove operations of LinkedPriorityQueue must satisfy the following requirements:

Elements of the queue always removed at the head of the list that is, head will be the front of the queue (like in the LinkedQueue)

The highest priority elements are stored in the initial section of the list, the next priority level follows the initial section etc. You can visualize this list such that each priority level is a linked list of its own, and then the lists are appended one after each other in the order of decreasing priority

A rear reference must be maintained for each priority level, these are the tail nodes of the priority levels; except for 0 level the links in these tails are not null but rather the first node of the next lower priority (a deputy head)

Additional care is needed when some or all of the priority levels are empty

Adding an element to the queue must be done at the rear that corresponds to the priority of the element

Fields An array of nodes named rears; stores the tail of the priority groups as subarrays; if a priority group is empty, its rear reference is null

front; the head reference of the entire linked list

2. Methods

Constructor initializing the rears array

add( ): adds a new element to the list at the rear of the priority group where the element belongs to. Note that the nodes in the list do not know their priority level, the level is determined by their position in the list. For this reason the add( ) method takes two parameters, a String for data and an int for the priority level. Implementation significantly harder than that of the original add. If p is the priority level and front is null (empty list) or rears[p] is not null, addition is obvious. However, careful selection logic is needed when the priority group of level p is empty.

===========================================================================================================

My Code (removed all my getter and setter methods to shorten the length of the code....assume they are standard implementations)

Also any other methods aside from what I need help with are omitted.

public class LinkedPriorityQueue {

private int manyNodes = 0; private Node front; private Node rears[]; private Node rear; private int priority_level; private int highestPriority;

public LinkedPriorityQueue(Node a[]) { rears = a; highestPriority = (rears.length - 1);

}

public void add(String item, int priority) { if (isEmpty()) { // Insert first item. front = new Node(item, null); rear = front; rears[0] = rear; manyNodes++;

}

else if (priority == highestPriority) {

rear.addNodeAfter(item); rears[0] = rear; rear = rear.getLink(); }

else if (priority < highestPriority) {

String target = "bye"; Node cursor = rears[0];

for (cursor = front; cursor != null; cursor = cursor.getLink()) {

if (cursor.getData() == target) { rear.addNodeAfter(item); rears[1] = rear; } }

} }

public boolean isEmpty() { return (front == null); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!