Yes. The loop body will execute just once.
It is important to check that a method works at the boundaries of a data structure. Verify that traversal:
This is quite a bit to verify.
Often programmers miss one of these, to their peril.
Here is a program that checks that our traversal works with a one-node list.
Recall that the constructor for
Node initializes the
next pointer to null.
public class ChainMaker
{
public static void main ( String[] args )
{
Node node0 = new Node( 223 );
// Traverse the Linked List in a loop
Node p = node0;
while ( p != null )
{
System.out.print("" + p );
p = p.getNext();
}
System.out.println( "\nEnd of List");
}
}
How can you check that the method works with an empty list, one that has no nodes?