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;
}
}
}