import java.io.*; /* * * * * * * * Modified methods * ---------------- * * str_write(): recursion replaced with a while loop * str_length(): recursion replaced with a while loop * str_extract(): recursion replaced with a while loop * str_index(): recursion replaced with a while loop * str_concat(): recursion replaced with a for loop * that uses the value returned from str_length() as * str_delete(): This one is a little complecated. Since the * the list is generated backwards by the method * I wrote I had to reverse is again before returning it. * The recursion has been replaced mainly with a while loop. * */ public class LLString { private char first; private LLString next; public LLString(char c, LLString s) { first = c; next = s; } public LLString() { first = ' '; next = null; } public LLString(String s) { StringBuffer sbuff = new StringBuffer(s); LLString result = null; for(int pos = sbuff.length() - 1; pos > 0; pos--) result = new LLString(sbuff.charAt(pos), result); next = result; first = sbuff.charAt(0); } void str_write() { LLString current; current = next; System.out.print(first); while(current != null){ System.out.print(current.first); current = current.next; } System.out.println(""); //All old code commented out for easy testing /* System.out.print(first); if(next == null) System.out.println(""); else next.str_write(); */ } int str_length() { LLString current; current = next; int size = 1; while(current != null){ size++; current = current.next; } return size; //All old code commented out for easy testing /* if(next == null) return 1; else return (1 + next.str_length()); */ } char str_extract(int pos) { LLString current; int count=0; if(pos == count){ return this.first; }else{ current = next; while(current != null){ count++; if(count == pos){ return current.first; } current = current.next; } return '\0'; } //All old code commented out for easy testing /* if(pos == 0) return this.first; else if (next == null) return (char)0; else return next.str_extract(pos-1); */ } int str_index(char c) { LLString current; int pos = 0; if(first == c){ return pos; }else{ current = next; while(current != null){ pos++; if(current.first == c){ return pos; } current = current.next; } return -1; } //All old code commented out for easy testing /* if(c == first) return 0; else if(next == null) return - 1; else { int pos = next.str_index(c); if(pos == -1) return -1; else return pos + 1; } */ } LLString str_concat(LLString nextString) { LLString current; current = next; int size = next.str_length(); for(int i = 0;i