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.

So what did the compiler show you? Do you want us to guess?
I guess
work?
this
Will

do I win the cigar?
(Although anyone who deliberately defines their own public class with the same name as a java.util class deserves any and all trouble they get)

According to your comment, hasNext() is supposed to return true if there are more elements in the list, but the code returns true when cursor.next == null. Isn't that backwards? Also in next(), you check if hasNext() is true, then you say there is no such element. This also seems backwards.

I did change the hasNext() method
and the compiler just shows
works?
this

the compiler didn't show "will", and I dont know why

In your next() method, you first return the data at cursor, and then set cursor to cursor.next. The comment in hasNext says it returns true if there are more elements in the list that have not been returned, but you are checking if cursor.next is null. The problem is you haven't yet returned the data at cursor by the time you are checking cursor.next.

To fix this, I recommend checking if cursor is null, rather than checking if cursor.next is null.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.