Question: Complete the implementation of the Iterator class that can traverse a list both forwards and backwards. public class Iterator { private LinkedList myList; private LinkedList

Complete the implementation of the Iterator class that can traverse a list both forwards and backwards.

public class Iterator {

private LinkedList myList; private LinkedList.Node myCurrentNode;

public Iterator(LinkedList list) { myList = list; myCurrentNode = myList.itsFirstNode; }

// return true if there is a "next" element, otherwise returns false public boolean hasNext() { if (myCurrentNode != null) return true; return false; }

// TO BE IMPLEMENTED /* // return true if there is a "prior" element, otherwise returns false public boolean hasPrior() {

} */

// returns the "next" node (really the current one) and // moves the Iterator forward by one node public T next() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getNextNode(); return data; }

// TO BE IMPLEMENTED /* // returns the "prior" node (really the current one) and // moves the Iterator backward by one node public T prior() {

}

// Sets this iterator to point to the last Node in the list public void setToEnd() {

} */ }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!