Below is my LinkedList class and also a private Node class inside it, I also write a unit test method to test if every method in LinkedList work, but the hasNext() and Next() ultimately failed.
Please let me know what is wrong with the next() and hasNext() method.
Here is the program:
public class LinkedList<E>
{
private static class Node<E>
{
public E data;
public Node<E> next;
public Node(E data, Node<E> next)
{
this.data = data;
this.next = next;
}
}
private Node<E> head;
// the number of elements in the list
private int count;
public LinkedList()
{
head = null;
count = 0;
}
public void addToFront(E e)
{
head = new Node<E>(e, head);
count++;
}
public void remove(E e)
{
Node<E> previous = null;
Node <E>current = head;
while (current != null)
{
if(current.data.equals(e))
{
current = current.next;
if(previous == null)
{
previous = current;
}
else
{
previous.next = current;
count--;
}
}
else
{
previous = current;
current = current.next;
}
}
count--;
}
// size() returns the number of elements in this list.
public int size()
{
return count;
}
public void clear()
{
head = null;
count = 0;
}
// iterator() returns a new iterator that has not yet returned any of
// the elements of this list.
public Iterator iterator()
{
return new Iterator();
}
public class Iterator
{
// you'll need to add fields here
public Node<E> cursor;
// The constructor initializes a new iterator that has not yet
// returned any of the elements of the list.
public Iterator()
{
cursor = head;
}
// hasNext() returns true if there are more elements in the list
// that have not been returned, and false if there are no more
// elements.
public boolean hasNext()
{
if(cursor.next == null)
{
return true;
}
return false;
}
public E next()
{
if(hasNext())
{
throw new NoSuchElementException("No next element");
}
E toReturn = cursor.data;
cursor = cursor.next;
return toReturn;
}
}
}
Here is the test case:
public class ForwardingProgram
{
public static void main(String[] args)
{
LinkedList<String> list = new LinkedList<String>();
list.addToFront("Will");
list.addToFront("this");
list.addToFront("work?");
LinkedList<String>.Iterator i = list.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
so the while loop should print out Will this work?
but my compiler didn't show that to me.
Please let me know what is wrong with it.