go to previous page   go to home page   go to next page highlighting

Answer:

It does not point to another Node. For it, next is null.


Traversal in a Loop

advancing the pointer

Linked lists can get very long. This calls for a loop. Here is our program (again) that now uses a loop to visit each Node.

public class ChainMaker
{
  public static void main ( String[] args )
  {
    Node node0 = new Node( 223 );  
    Node node1 = new Node( 493 );
    Node node2 = new Node( -47 );  
    Node node3 = new Node(  33 );
  
    node0.setNext( node1 );
    node1.setNext( node2 );
    node2.setNext( node3 );
    node3.setNext( null );  // not needed. Here as a reminder.
    
    // Traverse the Linked List in a loop
    Node p = node0;
    while ( _________________  )
    {
      System.out.print( p + ", " );
      p = p.getNext();
    }
    System.out.println( "\nEnd of List");
  }
}

As with many loops, there are three aspects that work together:

  1. The initial value of p must be set up correctly.
  2. The while must detect the ending condition.
  3. The change in p must be done correctly.

QUESTION 9:

The program is not quite complete. Loop aspect 2 is missing. What statement should go in the blank?

p = p.getNext();

p = node1;

p.getNext != null

p != null

go to previous page   go to home page   go to next page