Question: Java code: import java.util.Iterator; public class LinkedList { public void printReverse(){ printReverse(head); System.out.println(); } private void printReverse(Node n){ if(n != null){ printReverse(n.link); System.out.print(n.element+ );

 Java code: import java.util.Iterator; public class LinkedList { public void printReverse(){

printReverse(head); System.out.println(); } private void printReverse(Node n){ if(n != null){ printReverse(n.link); System.out.print(n.element+

" "); } } public static void main(String[] args) { String[] dwarfs

Java code:

import java.util.Iterator;

public class LinkedList {

public void printReverse(){

printReverse(head);

System.out.println();

}

private void printReverse(Node n){

if(n != null){

printReverse(n.link);

System.out.print(n.element+ " ");

}

}

public static void main(String[] args) {

String[] dwarfs = {"Happy", "Dopey", "Grumpy", "Sneezy", "Sleepy", "Bashful", "Doc"};

LinkedList list = new LinkedList();

for(String d : dwarfs) {

list.add(d);

}

list.print();

}

private class HappyLittleIterator implements Iterator {

public boolean hasNext() {

return false;

}

public E next() {

return null;

}

}

private class Node {

private E element; // Reference to element stored in this node

private Node link; // Reference to next node in list (or null if no next node)

public Node(E element) {

this.element = element;

}

}

private Node head; // Reference to first node in list (null if list empty)

private Node tail; // Reference to last node in list (null if list empty)

private int size; // Number of elements in this list

/**

* Default constructor - creates an empty list.

*/

public LinkedList() {

head = tail = null;

size = 0;

}

public int size() {

return size;

}

public E get(int index) {

if (index = size)

throw new IndexOutOfBoundsException(index+"");

Node walker=head;

for(int i=0; i

return walker.element;

}

public void add(E element) {

add(size, element);

}

public void add(int index, E element) {

if (index size)

throw new IndexOutOfBoundsException(index+"");

Node n = new Node(element);

if(size == 0) {

head = tail = n;

} else if (index == 0) {

n.link = head;

head = n;

} else if (index == size) {

tail.link = n;

tail = n;

} else {

Node walker = head;

for(int i=0; i

n.link = walker.link;

walker.link = n;

}

size++;

}

public E remove(int index) {

if (index = size)

throw new IndexOutOfBoundsException(index+"");

E doomed = null;

if (index == 0) {

doomed = head.element;

head = head.link;

if (head == null)

tail = null;

} else {

Node walker=head;

for(int i=0; i

doomed = walker.link.element;

walker.link = walker.link.link;

if (walker.link == null)

tail = walker;

}

size--;

return doomed;

}

public void print() {

for(Node walker=head; walker!=null; walker=walker.link) {

System.out.print(walker.element+" ");

}

System.out.println();

}

}

Task 1: Implement the recursive printReverse method that I introduced in class (reproduced below) and modify the main method to tst hthe method actully works. public void printReverseO 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? Ofcourse 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

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!