JAVA Question: LINKED LIST Here, on the first part we are inserting 500 and then
ID: 3601696 • Letter: J
Question
JAVA Question: LINKED LIST
Here, on the first part we are inserting 500 and then on second part removing 5. I am not understanding the "while loop" part on both. Can you comment out on each line inside while loop and explain in detail for both while loop what is happening, and how they are inserting and removing the integer. Thank you!!
l.listHeadInsert(new Integer(500));
System.out.println("After the head insert(500,3,4,5,6)");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
{
Node tmp = l.getCurrent();
Integer n = tmp.getValue();
System.out.println(n.intValue());
l.advance();
}
l.listRemove(new Integer(5));
System.out.println("After the remove (500,3,4,6)");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
{
Node tmp = l.getCurrent();
Integer n = tmp.getValue();
System.out.println(n.intValue());
l.advance();
}
Explanation / Answer
// while loop is used to print the elements in linked list.
/* l.isAtEnd() returns true if linkedlist has no elements.
Placing ! for this makes the condition true for all the inner nodes except the last one.
so the loop continues till the last. */
while(!l.isAtEnd())
{
//copy the current node to tmp.
Node tmp = l.getCurrent();
//get the integer value of current node
Integer n = tmp.getValue();
//print the current node value
System.out.println(n.intValue());
//move to next node.
l.advance();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.