-
November 16th, 2011, 09:37 PM
#1
LinkedList Iterator
For an assignment, I had to create my own LinkedList class -- called BasicLinkedList -- that has many methods similar to LinkedList. I have to create a BasicListIterator that can iterate through the list, which contains the next(), previous(), hasNext(), and hasPrevious() methods. I'm having trouble with this, and keep getting NullPointerExceptions whenever I try to test it. Please let me know what I'm doing wrong!!
<CODE>
private class Node
{
public E data;
public Node next;
public Node prev;
}
private Node head;
private class MyListIterator implements BasicListIterator<E>
{
private Node position = head;
public E next()
{
if(!(hasNext()))
{
throw new NoSuchElementException();
}
else
{
E data = position.data;
position = position.next;
return data;
}
}
public E previous()
{
if(!(hasPrevious()))
{
throw new NoSuchElementException();
}
else
{
System.out.println(position);
E data = position.prev.data;
position = position.prev;
return data;
}
}
public boolean hasNext()
{
if(position == null)
{
return false;
}
else
{
return true;
}
}
public boolean hasPrevious()
{
if(position == head)
{
return false;
}
else
{
return true;
}
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
</CODE>
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|