public class LinkedListWithIteratorDemo2 {
public static void main(String[] args) {
String BadString = "Thrie";
String FiveString = "Five";
String SixString = "Six";
StringLinkedListWithIterator list = new StringLinkedListWithIterator();
list.addANodeToStart("One");
list.addANodeToStart("Two");
list.addANodeToStart("Thrie");
list.addANodeToStart("Four");
list.resetIteration();
try {
while (list.length() >= 0) {
if (BadString.equals(list.getDataAtCurrent()))
list.deleteCurrentNode();
else
list.goToNext();
}
} catch (LinkedListException e) {
if (e.getMessage().equals("Iterating with an empty list.")) {
System.out.println("Fatal error.");
System.exit(0);
}
}
// Ãß°¡µÈ ¸Þ¼Òµå public void addANodeToEnd(Sting addString)
list.addANodeToEnd(FiveString);
list.addANodeToEnd(SixString);
System.out.println("Start of list: ");
list.showList();
System.out.println("End of list.");
}
}
¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ
À§¿¡ º¸ÀÌ´Â addANodeToEnd¸¦ ±¸ÇöÇؾßÇÕ´Ï´Ù
¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ
public class StringLinkedListWithIterator {
private ListNode head;
private ListNode current;
private ListNode previous;
public StringLinkedListWithIterator() {
head = null;
current = null;
previous = null;
}
public int length() {
int count = 0;
ListNode = head;
while (!= null) {
count++;
= }
return count;
}
public void addANodeToStart(String addData) {
head = new ListNode(addData, head);
if (current == head.link && current != null)
previous = head;
}
public void addANodeToEnd(String addData) {
resetIteration();
try {
while (moreToIterate())
goToNext();
if (head == null)
addANodeToStart(addData);
else {
¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ
±¸ÇöÇÒ ºÎºÐÀÔ´Ï´Ù.
°øºÎ½ÃÀÛÇÑÁö µÎ´ÞÀÌ ³Ñ¾ú´Âµ¥ ¼ö¾÷¹®Á¦µµ ¾ÆÁ÷Àº ¾î·Æ³×¿ä..
¿©±âÀú±â ã¾Æº¸°í ±¸ÇöÇغÁµµ ¿¡·¯°¡ ¶°¼ Áú¹®À» ¿Ã·Áº¾´Ï´Ù.
¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ¤Ñ
}
} catch (LinkedListException e) {
System.out.println(e.getMessage());
System.exit(0);
}
}
public boolean onList(String target) {
return (find(target) != null);
}
private ListNode find(String target) {
ListNode = head;
String dataAt while (!= null) {
dataAt= if (dataAt return = }
return null;
}
public void showList() {
ListNode = head;
while (!= null) {
System.out.println( = }
}
public String[] arrayCopy() {
String[] a = new String[length()];
ListNode = head;
int i = 0;
while (!= null) {
a[i] = i++;
= }
return a;
}
public void resetIteration() {
current = head;
previous = null;
}
public void goToNext()
throws LinkedListException {
if (current != null) {
previous = current;
current = current.link;
} else if (head != null) {
throw new LinkedListException(
"Iterated too many times or uninitialized iteration.");
} else {
throw new LinkedListException("Iterating with an empty list.");
}
}
public boolean moreToIterate() {
return (current != null);
}
public String getDataAtCurrent()
throws LinkedListException {
if (current != null)
return (current.data);
else
throw new LinkedListException(
"Getting data when current is not at any node.");
}
public void resetDataAtCurrent(String newData)
throws LinkedListException {
if (current != null)
current.data = newData;
else
throw new LinkedListException(
"Setting data when current is not an any node.");
}
public void insertNodeAfterCurrent(String newData)
throws LinkedListException {
ListNode newNode = new ListNode();
newNode.data = newData;
if (current != null) {
newNode.link = current.link;
current.link = newNode;
} else if (head != null) {
throw new LinkedListException(
"Inserting when iterator is past all "
+ "nodes or uninitialized iterator.");
} else {
throw new LinkedListException(
"Using insertNodeAfterCurrent with empty list.");
}
}
public void deleteCurrentNode()
throws LinkedListException {
if ((current != null) && (previous != null)) {
previous.link = current.link;
current = current.link;
} else if ((current != null) && (previous == null)) {
head = current.link;
current = head;
} else {
throw new LinkedListException(
"Deleting with uninitialized current or an empty list.");
}
}
private class ListNode {
private String data;
private ListNode link;
public ListNode() {
link = null;
data = null;
}
public ListNode(String newData, ListNode linkValue) {
data = newData;
link = linkValue;
}
}
}
|