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

Answer:

There is no size limit. Linked lists keep growing as long as you keep inserting (Although your computer may run out of memory.)

This is one of the advantages of linked lists over arrays.


Testing Program

Here is a testing program. It asks how many nodes the user wants, then creates a linked list of random integers in the range 0 to 1000. To run it, put it in a file LinkedListTester2.java in the same directory (or BlueJ project) as Node.java and LinkedList.java.

// LinkedListTester2
//
import java.util.* ;

public class LinkedListTester2
{
  public static void main( String[] args )
  {
    LinkedList list = new LinkedList();
    Scanner scan = new Scanner( System.in );
    Random rand = new Random();
    
    System.out.print("How many nodes? ");
    int numNodes = scan.nextInt();
    for ( int j = 0; j<numNodes; j++ )
    {
      list.insertFirst( rand.nextInt(1001) );
    }
    list.traverse();  
  }
}
linked list traversal
C:\JavaSource> javac LinkedListTester2.java LinkedList.java Node.java
C:\JavaSource> java  LinkedListTester2
How many nodes? 10
823, 396, 52, 733, 582, 655, 387, 577, 244, 431,
C:\JavaSource>

You may wish to modify traverse() so that it outputs a newline character "\n" every 10 numbers.


QUESTION 10:

How would you remove the first node of a list?


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