Task 1: Implement the recursive printReverse method that I introduced in class (reproduced
below) and modify the main method to test that the method actually works.
public void printReverse() {
printReverse(head);
System.out.println();
}
private void printReverse(Node n) {
if (n != null) {
printReverse(n.link);
System.out.print(n.element+” “);
}
}
So, does it work or am I just making crap up? Just for giggles, switch the order of the two lines
in the if statement and run the program. What happens?
Task 2: Add a for-each loop to the main method and print all of the elements in the list. Does it
work? Of course not! The for-each loop uses an iterator and our list doesn’t have one. Guess
what you’re about to do? Fun!
Each data structure that supports a for-each loop must create its own iterator. To do this, you
will write a class inside the LinkedList class:
private class HappyLittleIterator implements Iterator
This inner class declares itself to be an iterator by implementing Java’s Iterator interface. In
order for a class to implement Iterator, the class must include the methods next and hasNext.
The next method returns the “next” element in the list. The hasNext element just indicates
whether or not there is a next element.
(continued next page)
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
return null;
}
(Note the next method throws the NoSuchElementException if there is no next element.) In
addition to these two methods, the HappyLittleIterator class must include a walker as a field and
a constructor that initializes the walker. The walker will always be on the “next” element except
when it’s null, in which case there is no next element.
Complete the hasNext and next methods.
Once your HappyLittleIterator class is complete, your for-each loop will still not work. The
LinkedList class needs to declare its support for the for-each loop by implementing the Iterable
interface. (That’s Iterable, not Iterator!)
public class LinkedList
Finally, add the following method to the LinkedList class:
public Iterator
return new HappyLittleIterator();
}
If you did everything correctly, the for-each loop in your main method will work.
Task 3: Implement a method in the LinkedList class that reverses the elements in the list.
1. This does not mean I want you to print the list in reverse…you must actually
reverse the order of the elements in the list.
2. The method must be O(n), where n is the size of the list.
3. You may not use a secondary data structure (i.e. no array, stack, etc.)
4. You may not create any new node objects. (But you may create walkers.)
5. You are not creating a new list…you are reversing the existing one.
6. You are not expected to use recursion…you can do this with a while loop.
HINT: Draw a picture of a specific list and see how you might reverse the nodes.
HINT: You can use more than one walker.
Task 4: TEST YOUR PROGRAM!!!
List a series of tests to perform to validate your program.
Implement and run a test method.
Was the test successful? If not, return to Task 1 and fix the problem. Then, test
again!
Submit both printed and electronic copies of your lab. Just print the methods you wrote; do not
print the entire LinkedList class.
modify the linkedlist

0 comments