/* TestIntList.java Test the integer list class. Bruce M. Bolden May 18, 2000 Revised: December 1, 2003 http://www.cs.uidaho.edu/~bruceb/ */ import java.io.*; class TestIntList { public static void main( String[] args ) { IntList list1 = new IntList( 1 ); // , null System.out.println( list1.head() ); //IntList list2 = new IntList( 2, list1 ); //IntList list3 = new IntList( 3, list2 ); //list1 = new IntList( 2, list1 ); //list1 = new IntList( 3, list1 ); //PrintIntList( list3 ); list1 = new IntList( 3, new IntList( 2, list1 )); System.out.print( "list1: " ); list1.print(); System.out.print( "list1: " ); list1.print(); } } /* IntList.java */ class IntList { int value; IntList next; // Constructors IntList( int x ) { value = x; next = null; } IntList( int x, IntList aList ) { value = x; next = aList; } /** int value: data at front of list */ int head() { return value; } /** list: remainder of list */ IntList tail() { return next; } boolean empty() { return( this == null ); } void print() { System.out.println( "print()" ); if( this != null ) { System.out.print( "{ " ); System.out.print( value ); IntList tmpList = next; while( tmpList != null ) //while( !tmpList.empty() ) { System.out.print( ", " ); System.out.print( tmpList.head() ); tmpList = tmpList.tail(); } System.out.println( " }" ); } } }