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

Answer:

Of course. Each call to insertFirst() inserts a new node at the beginning of the list.


Traverse

Here is class LinkedList again, now with a traverse() method added:

public class LinkedList
{
   private Node headPtr = null;
  
  // The constructor creates an empty list
  public LinkedList()
  {
    headPtr = null;
  }
   
  // Insert one Node containing data at the head
  // of the list.  This will be explained in a few pages.
  public void insertFirst( int data )
  {
    Node newFirst = new Node( data );
    newFirst.setNext( headPtr );
    headPtr = newFirst;
  }
   
  // Traverse the list, printing each node
  public void traverse()
  {
    Node p = headPtr;
    while ( p != null )
    {
      System.out.print( p );
      p = p.getNext();
    }
  }
     
}

Say that traverse() has been called and that the list is empty.


QUESTION 5:

What will traverse() do with an empty list?