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.
java sorting data-structures linked-list traversal
add a comment |
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.
java sorting data-structures linked-list traversal
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
add a comment |
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.
java sorting data-structures linked-list traversal
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
java sorting data-structures linked-list traversal
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
add a comment |
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
add a comment |
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;
why anewNode
? 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
add a comment |
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.
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
add a comment |
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;
why anewNode
? 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
add a comment |
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;
why anewNode
? 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
add a comment |
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;
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;
answered Nov 11 at 17:58
Mureinik
177k22127196
177k22127196
why anewNode
? 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
add a comment |
why anewNode
? 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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
You can return the head of the linked list as:
return first;
This can be helpful to access the updated list again.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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