LinkedList Iterator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: LinkedList Iterator

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    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>

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: LinkedList Iterator

    Code tags require square brackets.

    I'm having trouble with this, and keep getting NullPointerExceptions whenever I try to test it.
    This code won't even compile. Please make sure you have posted the code you are running and also post the exceptions you are getting and explain when they occur..
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center