0

I'm trying to insert a request and sort it via priority, so highest(1) is first in the list.

public Node addByPriority(Object request, int priority) {
    size++;
    //creates a new node with a priority, owner and creator and sets its next node to the root
    Node newNode = new Node(request, priority);
    //node to store prev
    Node prevNode = null;
    //node to store current
    Node currNode = first;

    //cycle thru the nodes til either the priority is higher or current is null
    while (currNode != null && priority >= currNode.getPriority()) {
        prevNode = currNode;
        currNode = currNode.getNext();
    }
    if (prevNode == null) {
        newNode.setNext(first);
        first = newNode;
    }
    else {
        prevNode.setNext(newNode);
        newNode.setNext(currNode);
    }
    // what would be the return statement??
}  

It says I need a return statement but not sure what has to be put, or if there's another way.

1
  • 3
    We don't know either. It's up to you to decide what this method is supposed to return. If it doesn't need to return anything, then change the return type to void. If you want it to return the node created for the new object, then return that node. If you want it to return another node, then decide which node it's supposed to return, and return it. Commented Nov 11, 2018 at 17:57

2 Answers 2

1

You didn't state what Node you're supposed to return, but it stands to reason that you'd return the newly created one:

return newNode;
Sign up to request clarification or add additional context in comments.

4 Comments

why a newNode? and I mean why not the head/first ?
It should add the node to the list but for some reason its not doing that
@Mureinik as i said earlier, why not the head ?
@nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
0

You can return the head of the linked list as:

return first;

This can be helpful to access the updated list again.

1 Comment

Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.