For my project in my class I have to provide a remove method for the inner class IteratorForLinkedList. Note you will needanother data field. Include interface definition, class implementing the interface, and the test program. I have tried my best I just want to make sure I did what the assignment was done correclty. Here is what I have done already.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedListWithIterator<T> implements ListWithIteratorInterface<T> {
private Node firstNode;
private int numberOfEntries;
public LinkedListWithIterator() {
initaliazeDataFields();
}
/**
* Implementation for the ft list go here chapter 12, starting at
* segment 12.7
* @return
*/
public void clear() {
initaliazeDataFields();
}
private void initaliazeDataFields() {
firstNode = null;
numberOfEntries = 0;
}
private Node getNodeAt(int givenPosition) {
Node currentNode = firstNode;
for (int counter = 1; counter < givenPosition; counter++)
currentNode = currentNode.getNextNode;
return currentNode;
}
public void add(T newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty())
firstNode = newNode;
else {
Node lastNode = getNodeAt(numberOfEntries);
lastNode.setNextNode(newNode);
}
numberOfEntries++;
}
public void add(int givenPosition, T newEntry) {
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries + 1)) {
Node newNode = new Node(newEntry);
if(givenPosition == 1) {
newNode.setNextNode(firstNode);
firstNode = newNode;
} else {
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeAfter = nodeBefore.getNextNode();
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
}
numberOfEntries++;
} else
throw new IndexOutOfBoundsException("illegal position given to add operation");
} // end add
public boolean isEmpty() {
boolean result;
if (numberOfEntries == 0) {
result = true;
} else {
result = false;
}
return result;
}
public T[] toArray() {
@SuppressWarnings("unchecked")
T[] result = (T[])new Object[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode != null)) {
result[index] = currentNode.getData();
currentNode = currentNode.getNextNode();
index++;
}
return result;
} // end to array
public T remove(int givenPosition) {
T result = null;
if((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
if (givenPosition == 1) {
result = firstNode.getData();
firstNode = firstNode.getNextNode();
} else {
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeToRemove = nodeBefore.getNextNode();
result = nodeToRemove.getData();
Node nodeAfter = nodeToRemove.getNextNode();
nodeBefore.setNextNode(nodeAfter);
}
numberOfEntries--;
return result;
} else {
throw new IndexOutOfBoundsException("illegal position given to remove ");
}
} // end remove
public T replace(int givenPosition, T newEntry) {
if((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
Node desiredNode = getNodeAt(givenPosition);
T originalEntry = desiredNode.getData();
return originalEntry;
}
else
throw new IndexOutOfBoundsException("Illegal Position given for replace method ");
} // end replace
public T getEntry(int givenPosition) {
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
return getNodeAt(givenPosition).getData;
}
else
throw new IndexOutOfBoundsException("Illegal Position given for getentry method ");
} // end get entry
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while(!found && (currentNode != null)) {
if(anEntry.equals(currentNode.getData())) {
found = true;
} else {
currentNode = currentNode.getNextNode;
}
}
return found;
}
public Iterator<T> iterator() {
return new IteratorForLinkedList();
}
public Iterator getIterator() {
return iterator();
}
private class IteratorForLinkedList implements Iterator<T> {
private Node nextNode;
private IteratorForLinkedList() {
nextNode = firstNode;
}
public T next() {
T result;
if (hasNext()) {
result = nextNode.getData;
} else {
throw new NoSuchElementException("Illegal call to next(); iterator is after end of list");
}
return result;
}
public boolean hasNext() {
return nextNode != null;
}
public void remove() {
throw new UnsupportedOperationException("Remove() is not supported by this operator");
}
}
// Implementation for private class node
private class Node
{
public T getData;
public LinkedListWithIterator<T>.Node getNextNode;
private T data;
private Node next;
private Node(T dataPortion) {
this(dataPortion, null);
}
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
}
private T getData() {
return data;
}
private void setData(T newEntry) {
data = newEntry;
}
private Node getNextNode() {
return next;
}
private void setNextNode(Node nextNode) {
next = nextNode;
}
} // end node class
}