0

I'm having trouble implementing a method to retrieve the information in a node given its location. For example, a location of 1 would return the head. A location of 2 would return the node just before the head.

if (location ==1)
 return top();
else 
 for (int i =1; i < LinkedStack.size(); i++){
 return LLNode.getInfo(location);
 }
return null;

^Thats what I have but its completely wrong.

asked Aug 28, 2015 at 21:32

1 Answer 1

1

Just loop through the list so do something like:

current = top();
for (int i = 1; i < location ; i++){
 current = current.next;
}
return current;

you may also want to add an if statement so if the location is larger than the list than return a message or something

answered Aug 28, 2015 at 21:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.