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

Answer:

Node 0: ant  

Linked List of StringNodes

one node, not linked

This program builds a linked list of StringNodes.

There are 12 objects in the picture. A StringNode object here is a discrete object, not actually contained in the StringNode that points to it. Sometimes people describe the structure as "a linked list of Strings", but what they really should say is "a linked list of nodes that point to String objects."

public class StringNodeTraverse
{
  public static void main ( String[] args )
  {
    StringNode node0 = new StringNode( "ant" );  
    
    StringNode node1 = new StringNode( "bat" );
    node0.setNext( node1 );
    
    StringNode node2 = new StringNode( "cat" );  
    node1.setNext( node2 );
    
    StringNode node3 = new StringNode( "dog" );  
    node2.setNext( node3 );
    
    StringNode node4 = new StringNode( "eel" );  
    node3.setNext( node4 );
    
    StringNode node5 = new StringNode( "fox" );  
    node4.setNext( node5 );
       
    // Traverse the Linked List in a loop
    StringNode p = node0;
    
    while (  p != null  )
    {
      System.out.print( p + ", " ) ;
      
      p = p.getNext();
    }
    
  }
}

The program displays:

ant, bat, cat, dog, eel, fox, 

QUESTION 17:

Must the data in a node type only consist of two members: a value and a next pointer?


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