/** * CharList * * A linked list of characters. Your job is to complete the incomplete * methods. Two methods (the constructor and print) are * already complete. One (set) is complete except for the * javadoc comment. The rest have complete javadoc comments, but only * stub implementations. *

* I have used main to provide a small unit test for the * CharList, print, and set methods. * You should expand this test as you complete the methods. * * @author Jeff Ondich (so far) * */ class CharList { public CharNode head; /** * Unit test for the CharList methods. */ public static void main( String[] args ) { CharList testList = new CharList(); if( args.length > 0 ) testList.set( args[0] ); testList.printRecursive( testList.head ); System.out.println(); testList.printBackwards( testList.head ); System.out.println(); } /** * Initializes the list to empty. */ public CharList() { head = null; } public void printRecursive( CharNode front ) { if( front == null ) return; System.out.print( front.data ); printRecursive( front.next ); } public void printBackwards( CharNode front ) { if( front == null ) return; printBackwards( front.next ); System.out.print( front.data ); } /** * Prints the contents of the list to standard output. */ public void print() { CharNode current = head; while( current != null ) { System.out.print( current.data ); current = current.next; } } /** * ??? * * @param ??? */ public void set( String s ) { head = null; int length = s.length(); for( int k=s.length()-1; k >= 0; k-- ) { CharNode newNode = new CharNode( s.charAt( k ) ); newNode.next = head; head = newNode; } } /** * Inserts the specified character at the specified position in the list. * For example, if index is 3, the specified character will become * the fourth character in the list. If index is larger than the * current length of the list, the character is appended to the end of the list. * If index is negative, the character is inserted at the beginning * of this list. * * @param index the position at which to insert the character * ch the character to insert */ public void add( int index, char ch ) { } /** * Appends the given character to the end of this list. * * @param ch the character to add to this list */ public void add( char ch ) { } /** * Appends a copy of the specified list to the end of this list. * * @param list the list to copy and append to the end of this list */ public void add( CharList list ) { } /** * Returns the number of characters in this list. * * @return the length of this list */ public int length() { return 0; } /** * Returns the character at the given index. * * @param index the index of the character to retrieve * @return the character at the given position, or the null character * if the index is out of range */ public char get( int index ) { return '\0'; } /** * Returns the index of the first occurrence of the specified character, or -1 * if this list does not contain the specified character. * * @param ch the character to search for * @return the index of the first occurrence of the specified character in this * list, or -1 if the character is not found */ public int indexOf( char ch ) { return -1; } }