import java.io.*; class UseCharListsA7 { // Shows constructive append and change on lists public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); CharListA L1=null,L2=null,L3; 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 constructive append "); L3=conAppend(L1,L2); System.out.print("The lists are now: "); CharListAIO.print(L1); System.out.print(" and "); CharListAIO.print(L2); System.out.print(" and "); CharListAIO(L3); 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 constructive change of first to "); System.out.println("second character"); System.out.println("in second list "); L3=conChange(ch1,ch2,L2); System.out.print("The lists are now: "); CharListAIO.print(L1); System.out.print(" and "); CharListAIO.print(L2); System.out.print(" and "); CharListAIO.print(L3); System.out.println(); } public static CharListA conAppend(CharListA L1,CharListA L2) { if(L1==null) return L2; else { CharListA L3 = conAppend(L1.tail,L2); return new CharListA(L1.head,L3); } } public static CharListA conChange(char ch1,char ch2,CharListA L) { if(L==null) return null; else { CharListA T=conChange(ch1,ch2,L.tail); char h=L.head; if(h==ch1) return new CharListA(ch2,T); else return new CharListA(h,T); } } }