Linked List sorted insertion









up vote
0
down vote

favorite












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.










share|improve this question



















  • 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.
    – JB Nizet
    Nov 11 at 17:57














up vote
0
down vote

favorite












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.










share|improve this question



















  • 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.
    – JB Nizet
    Nov 11 at 17:57












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question















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.







java sorting data-structures linked-list traversal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:06









Mani

1271215




1271215










asked Nov 11 at 17:53









RHH

24




24







  • 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.
    – JB Nizet
    Nov 11 at 17:57












  • 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.
    – JB Nizet
    Nov 11 at 17:57







3




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.
– JB Nizet
Nov 11 at 17:57




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.
– JB Nizet
Nov 11 at 17:57












2 Answers
2






active

oldest

votes

















up vote
1
down vote













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;





share|improve this answer




















  • why a newNode? and I mean why not the head/first ?
    – nullpointer
    Nov 11 at 17:58











  • It should add the node to the list but for some reason its not doing that
    – RHH
    Nov 11 at 18:01










  • @nullpointer what else would you have it return?
    – Mureinik
    Nov 11 at 18:09










  • @Mureinik as i said earlier, why not the head ?
    – nullpointer
    Nov 11 at 18:11











  • @nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
    – Mureinik
    Nov 11 at 19:00

















up vote
1
down vote













You can return the head of the linked list as:



return first;


This can be helpful to access the updated list again.






share|improve this answer




















  • Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
    – nullpointer
    Nov 11 at 18:08










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251534%2flinked-list-sorted-insertion%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













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;





share|improve this answer




















  • why a newNode? and I mean why not the head/first ?
    – nullpointer
    Nov 11 at 17:58











  • It should add the node to the list but for some reason its not doing that
    – RHH
    Nov 11 at 18:01










  • @nullpointer what else would you have it return?
    – Mureinik
    Nov 11 at 18:09










  • @Mureinik as i said earlier, why not the head ?
    – nullpointer
    Nov 11 at 18:11











  • @nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
    – Mureinik
    Nov 11 at 19:00














up vote
1
down vote













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;





share|improve this answer




















  • why a newNode? and I mean why not the head/first ?
    – nullpointer
    Nov 11 at 17:58











  • It should add the node to the list but for some reason its not doing that
    – RHH
    Nov 11 at 18:01










  • @nullpointer what else would you have it return?
    – Mureinik
    Nov 11 at 18:09










  • @Mureinik as i said earlier, why not the head ?
    – nullpointer
    Nov 11 at 18:11











  • @nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
    – Mureinik
    Nov 11 at 19:00












up vote
1
down vote










up vote
1
down vote









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;





share|improve this answer












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;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 17:58









Mureinik

177k22127196




177k22127196











  • why a newNode? and I mean why not the head/first ?
    – nullpointer
    Nov 11 at 17:58











  • It should add the node to the list but for some reason its not doing that
    – RHH
    Nov 11 at 18:01










  • @nullpointer what else would you have it return?
    – Mureinik
    Nov 11 at 18:09










  • @Mureinik as i said earlier, why not the head ?
    – nullpointer
    Nov 11 at 18:11











  • @nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
    – Mureinik
    Nov 11 at 19:00
















  • why a newNode? and I mean why not the head/first ?
    – nullpointer
    Nov 11 at 17:58











  • It should add the node to the list but for some reason its not doing that
    – RHH
    Nov 11 at 18:01










  • @nullpointer what else would you have it return?
    – Mureinik
    Nov 11 at 18:09










  • @Mureinik as i said earlier, why not the head ?
    – nullpointer
    Nov 11 at 18:11











  • @nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
    – Mureinik
    Nov 11 at 19:00















why a newNode? and I mean why not the head/first ?
– nullpointer
Nov 11 at 17:58





why a newNode? and I mean why not the head/first ?
– nullpointer
Nov 11 at 17:58













It should add the node to the list but for some reason its not doing that
– RHH
Nov 11 at 18:01




It should add the node to the list but for some reason its not doing that
– RHH
Nov 11 at 18:01












@nullpointer what else would you have it return?
– Mureinik
Nov 11 at 18:09




@nullpointer what else would you have it return?
– Mureinik
Nov 11 at 18:09












@Mureinik as i said earlier, why not the head ?
– nullpointer
Nov 11 at 18:11





@Mureinik as i said earlier, why not the head ?
– nullpointer
Nov 11 at 18:11













@nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
– Mureinik
Nov 11 at 19:00




@nullpointer because adding an element won't necessarily change the head. It will however create a new node every time
– Mureinik
Nov 11 at 19:00












up vote
1
down vote













You can return the head of the linked list as:



return first;


This can be helpful to access the updated list again.






share|improve this answer




















  • Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
    – nullpointer
    Nov 11 at 18:08














up vote
1
down vote













You can return the head of the linked list as:



return first;


This can be helpful to access the updated list again.






share|improve this answer




















  • Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
    – nullpointer
    Nov 11 at 18:08












up vote
1
down vote










up vote
1
down vote









You can return the head of the linked list as:



return first;


This can be helpful to access the updated list again.






share|improve this answer












You can return the head of the linked list as:



return first;


This can be helpful to access the updated list again.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 17:59









nullpointer

38.1k1073146




38.1k1073146











  • Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
    – nullpointer
    Nov 11 at 18:08
















  • Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
    – nullpointer
    Nov 11 at 18:08















Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
– nullpointer
Nov 11 at 18:08




Inferred from few comments like able to print the whole list and from question so highest(1) is first in the list
– nullpointer
Nov 11 at 18:08

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251534%2flinked-list-sorted-insertion%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto