Created
April 11, 2012 03:36
-
-
Save Xe/2356670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList { | |
private class Node { | |
public char data; | |
public Node next; | |
Node(char d, Node n) { | |
data = d; | |
next = n; | |
} | |
} | |
Node head = null; | |
int size = 0; | |
public void add(char d) { | |
if(head == null) { | |
head = new Node(d, null); | |
} else { | |
Node curr; | |
for(curr = head; curr.next != null; curr = curr.next); | |
curr.next = new Node(d, null); | |
} | |
size++; | |
} | |
public void clear() { | |
size = 0; | |
head = null; | |
} | |
public String toString() { | |
String buf = ""; | |
for(Node curr = head; curr != null; curr = curr.next) { | |
buf = buf + curr.data; | |
} | |
return buf; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedListTester { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
LinkedList test = new LinkedList(); | |
test.add('d'); | |
test.add('a'); | |
test.add('m'); | |
test.add('n'); | |
System.out.printf("Size: %d, Contents: %s", test.size, test.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MutableString implements Comparable<MutableString> { | |
LinkedList list = new LinkedList(); | |
public MutableString(String data) { | |
for(char c : data.toCharArray()) { | |
list.add(c); | |
} | |
} | |
private void setString(String data) { | |
list.clear(); | |
for(char c : data.toCharArray()) { | |
list.add(c); | |
} | |
} | |
public char chatAt(int i) { | |
return list.toString().charAt(i); | |
} | |
public void set(int i, char data) throws MutableStringIndexOutOfBoundsException { | |
char[] variance = list.toString().toCharArray(); | |
try { | |
variance[i] = data; | |
} catch (Exception e) { | |
throw new MutableStringIndexOutOfBoundsException(); | |
} | |
setString(new String(variance)); | |
} | |
public void toUpper() { | |
setString(list.toString().toUpperCase()); | |
} | |
public void toLower() { | |
setString(list.toString().toLowerCase()); | |
} | |
public MutableString substring (int start, int finish) { | |
return new MutableString(list.toString().substring(start, finish)); | |
} | |
public int length() { | |
return list.size; | |
} | |
public char[] toCharArray() { | |
return list.toString().toCharArray(); | |
} | |
public String toString() { | |
return list.toString(); | |
} | |
public int compareTo(MutableString arg0) { | |
return list.toString().compareTo(arg0.toString()); | |
} | |
public String toReverseString() { | |
return ""; | |
} | |
public void remove(int i) { | |
String finals = list.toString().substring(0, i) + | |
list.toString().substring(i+1, list.size); | |
setString(finals); | |
} | |
public Character remove(char c) { | |
LinkedList temp = new LinkedList(); | |
char r = '\0'; | |
boolean removed = false; | |
for (char d : list.toString().toCharArray()) { | |
if(!removed && d == c) { | |
r = d; | |
removed = true; | |
continue; | |
} | |
temp.add(d); | |
} | |
list = temp; | |
return r; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MutableStringTester { | |
/** | |
* @param args | |
* @throws MutableStringIndexOutOfBoundsException | |
*/ | |
public static void main(String[] args) throws MutableStringIndexOutOfBoundsException { | |
// TODO Auto-generated method stub | |
MutableString test = new MutableString("This is a taco."); | |
System.out.println(test.chatAt(3)); | |
test.set(4, '2'); | |
System.out.println(test.toString()); | |
for (char c : test.toCharArray()) { | |
System.out.print(c + "\t"); | |
} | |
System.out.println(); | |
test.toUpper(); | |
System.out.println(test.toString()); | |
test.toLower(); | |
System.out.println(test.toString()); | |
MutableString sub = test.substring(4, 8); | |
System.out.println(sub.toString()); | |
test.remove('t'); | |
System.out.println(test.toString()); | |
test.remove(1); | |
System.out.println(test.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment