Problem

The class StringLinkedListWithIterator (Listing 12.9) is its own iterator, but it does not...

The class StringLinkedListWithIterator (Listing 12.9) is its own iterator, but it does not quite implement the Java Iterator interface. Redefine the class StringLinkedListWithIterator so that it implements the Iterator interface. This interface declares the methods next, remove, and hasNext as follows:

/**Returns the next element on the list.Throws a NoSuchElementException if there is no next element to return.*/public E next() throws NoSuchElementException/**Removes the last element that was returned most recently by the invocation next().Throws an IllegalStateException if the method next has not yet been called or if the method remove has already been called after the last call to the method next.*/public void remove() throws IllegalStateException/**Returns true if there is at least one more element for the method next to return. Otherwise, returns false.*/public boolean hasNext()

Notice the generic data type E in the specification of the method next.

If you follow the instructions in this question carefully, you do not need to know what an interface is, although knowing what one is may make you feel more comfortable.

Begin the class definition with

import java.util.Iterator;public class StringLinkedListWithIterator2implements Iterator‹String›{private ListNode head;private ListNode current;private ListNode previous; private ListNodetwoBack; private booleanremoveSinceNext;public StringLinkedListWithIterator2(){head = null;current = null;previous = null;twoBack = null;removeSinceNext = true;}

The rest of the definition is like the one in except that you add the method definitions specified by Iterator, make a small change to the method resetIteration so that the new instance variable twoBack is reset, and omit the methods deleteCurrentNode, goToNext, and moreToIterate, which become redundant.(Hints:

• Despite its pretentious-sounding details, this exercise is fairly easy. T he three method definitions you need to add are easy to implement using the methods we have.


• Note that the method hasNext and the method moreToIterate are not exactly the same.
• The exception classes mentioned are all predefined, and you should not define them. These particular exceptions do not need to be caught or declared in a throws clause. The Iterator interface says that the method remove throws an UnsupportedOperationException if the remove method is not supported. However, your method remove has no need ever to throw this exception.)

Listing 12.9 A Linked List with an Iterator

 /**Linked list with an iterator. One node is the "current node." Initially, the current node is the first node. It can be changed to the next node until the iteration has moved beyond the end of the list.*/public class StringLinkedListWithIterator{private ListNode head;private ListNode current;private ListNode previous;public StringLinkedListWithIterator(){head = null;current = null;previous = null;}public void addANodeToStart(String addData){head = new ListNode(addData, head);if ((current == head.link) && (current != null))//if current is at old start nodeprevious = head;}/**Sets iterator to beginning of list.*/public void resetIteration(){current = head;previous = null;}/**Returns true if iteration is not finished.*/public boolean moreToIterate(){return current != null;}/**Advances iterator to next node.*/public void goToNext(){if (current != null){previous = current;current = current.link;}else if (head != null){System.out.println("Iterated too many times or uninitializediteration.");System.exit(0);}else{System.out.println("Iterating with an empty list.");System.exit(0);}}/**Returns the data at the current node.*/public String getDataAtCurrent(){String result = null;if (current != null)result = current.data;else{System.out.println("Getting data when current is not at any node.");System.exit(0);}return result;}/**Replaces the data at the current node./*public void setDataAtCurrent(String newData){if (current != null){current.data = newData;}else{System.out.println("Setting data when current is not at any node.");System.exit(0);}}/**Inserts a new node containing newData after the current node. The current node is the same after invocation as it is before. Precondition: List is not empty; current node is not beyond the entire list.*/public void insertNodeAfterCurrent(String newData){ListNode newNode = new LisNode();newNode.data = newData;if (current != null){newNode.link = current.link;current.link = newNode;}else if (head != null){System.out.println("Inserting when iterator is past all " +"nodes or is not initialized.");System.exit(0);}else{System.out.println("Using insertNodeAfterCurrent with empty list.";System.exit(0);}}/**Deletes the current node. After the invocation, the current node is either the node after the deleted node or null if there is no next node.*/public void deleteCurrentNode(){if ((current != null) && (previous == null)){previous.link = current.link;current = current.link;}else if ((current != null) && (previous == null)){//At head nodehead = current.link;current = head;}else //current==null{System.out.println("Deleting with uninitialized current or an empty " +"list.");System.exit(0);}}The methods length, onList, find, and showList, as well as the private inner class ListNode are the same as inprivate class ListNode{private String data;private ListNode link;public ListNode(){link = null;data = null;}public ListNode(String newData, ListNode linkValue){data = newData;link = linkValue;}}The method toArray is the same as /**Returns an array of the elements on the list.*/public String[] toArray(){String[] anArray = new String[length()];ListNode position = head;int i = 0;while (position != null){anArray[i] = position.data;i++;position = position.link;}return anArray;}}

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT