import java.io.*; class UseCharListsA5 { // Shows destructive append and change on lists // using iteration public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); CharListA L1=null,L2=null; char ch1,ch2; try { System.out.print("Enter first list of characters: "); L1 = CharListAIO.read(in); System.out.print("Enter second list of characters: "); L2 = CharListAIO.read(in); } catch(ListFormatException e) { System.out.println("Error: "+e.getMessage()); } System.out.print("The lists are: "); CharListAIO.print(L1); System.out.print(" and "); CharListAIO.print(L2); System.out.println(); System.out.println("Call destructive append "); destAppend(L1,L2); System.out.print("The lists are now: "); CharListAIO.print(L1); System.out.print(" and "); CharListAIO.print(L2); System.out.println(); System.out.print("Enter first character: "); ch1=in.readLine().charAt(0); System.out.print("Enter second character: "); ch2=in.readLine().charAt(0); System.out.print("Call destructive change of first to "); System.out.println("second character"); System.out.println("in second list "); destChange(ch1,ch2,L2); System.out.print("The lists are now: "); CharListAIO.print(L1); System.out.print(" and "); CharListAIO.print(L2); System.out.println(); } public static void destAppend(CharListA L1,CharListA L2) { CharListA ptr; for(ptr=L1; ptr.tail!=null; ptr=ptr.tail); ptr.tail=L2; } public static void destChange(char ch1,char ch2,CharListA L) { for(CharListA ptr=L; ptr!=null; ptr=ptr.tail) if(ptr.head==ch1) ptr.head=ch2; } }