public class OurList <E> implements OurListInterface <E>
{

    //  I.  Local classes:

    //  PURPOSE:  To hold one data element in the linked list.

    class Node <E>
    {

    //  I.  Private member vars:
    E  item;
    Node next;
    Node prev;


    //  II.  Constructor(s):
    Node (E newItem)
    {
        item = newItem;
        next = prev = null;
    }


    //  III.  Accessors:
    E    getItems () { return(item); }
    Node getNext () { return(next); }
    Node getPrev () { return(prev); }


    //  IV.  Mutators:
    void setItem (E newItem) { item = newItem; }
    void setNext (Node newNext) { next = newNext; }
    void setPrev (Node newPrev) { prev = newPrev; }

    }


    //  PURPOSE:  To serve as an object that can iterate thru
    //            the Nodes of an OurList<E> instance.

    class OurListIterator <EE> implements OurListIteratorInterface <EE>
    {

    //  I.  Private vars:
    Node cursor;
    boolean isGoingForward;


    //  II.  Constructor:
    OurListIterator (Node newCursor, boolean newIsGoingForward)
    {
        cursor = newCursor;
        isGoingForward = newIsGoingForward;
    }


    //  III.  Methods that do main work of class:

    public boolean  atFirst ()
    {
        return( (cursor == null && isGoingForward)  ||
            (!isGoingForward && cursor == first)
          );
    }

    public boolean  atLast ()
    {
        return( (cursor == null && !isGoingForward) ||
            (isGoingForward && cursor == last)
          );
    }


    public EE  prev ()
    {

        if  (isGoingForward)
        {

        //  We WERE going forward, now we're switching direction:

        //  If we're already at beginning then there's nowhere to go:

        if  (atFirst())
            return(null);


        //  Just jump over the same item in opposite direction.
        //  No need to move cursor.

        isGoingForward = false;
        return((EE)cursor.getItems());
        }
        else
        {
        EE temp;


        //  If we're already at beginning then there's nowhere to go:

        if  (atFirst())
            return(null);


        //  Jump to next item:

        if  (cursor == null)
            cursor = last;
        else
            cursor = cursor.getPrev();


        //  Now return the item to which we just jumped:

        return((EE)cursor.getItems());
        }

    }


    public EE  next ()
    {

        if  (isGoingForward)
        {
        EE temp;

        //  If we're already at end then there's nowhere to go:

        if  (atLast())
            return(null);


        //  Jump to next item:

        if  (cursor == null)
            cursor = first;
        else
            cursor = cursor.getNext();


        //  Now return the item to which we just jumped:

        return((EE)cursor.getItems());
        }
        else
        {

        //  We WERE going backward, now we're switching direction:

        //  If we're already at end then there's nowhere to go:

        if  (atLast())
            return(null);


        //  Just jump over the same item in opposite direction.
        //  No need to move cursor.

        isGoingForward = true;
        return((EE)cursor.getItems());
        }

    }


    //  PURPOSE:  To set the just-jumped-over Node to hold 'newValue'.
        //        No return value.
    public void set (EE newValue)
    {
        if  (cursor == null)
        return;

        cursor.setItem(newValue);
    }


    //  PURPOSE:  To (conceptually) add a Node to hold 'toAdd' to the
        //      inbetween location to which the cursor points.  This means
        //      (for isGoingForward==true) that 'toAdd' is added *before*
        //      (previous to) the position of 'cursor'.  'cursor' stays pointing
        //      to the same Node as before.  If 'cursor' was 'null' (at the
    //       beginning of the list) then 'cursor' will point the the old
        //      list 'first' (which no longer will be the beginning because
        //      'toAdd' would have been added before it.)
        //      For isGoingForward==false 'toAdd' is added *after* (next to)
        //      the position of 'cursor'.  'cursor' stays pointing to the same
        //      Node as before.  If 'cursor' was 'null' (at the end of the list)
    //      then 'cursor' will point the the old list 'last' (which no
        //       longer will be the last because 'toAdd' would have been added
        //      after it.)
    public void add (EE toAdd)
    {
    }


    //  PURPOSE:  To (conceptually) remove the Node that we just jumped
    //      over.  This means (for both isGoingForward == true and == false)
    //      to remove the Node to which 'cursor' points (or to do nothing
        //      if 'cursor' is 'null').  When a Node actually is removed we
        //      set 'cursor' to the *next* item in the list if
        //      isGoingForward==true or to the *previous* item in the list if
        //      isGoingForward==false.
    public void remove ()
    {
    }

    }


    //  II.  Member vars:

    Node first;

    Node last;

    int size;


    //  III.  Constructor(s):

    OurList ()
    {
    first = last = null;
    size = 0;
    }


    //  IV.  Methods that do main work:

    public boolean isEmpty ()
    {
    return(size == 0);
    }


    public void addFirst (E toAdd)
    {
    Node newNode = new Node(toAdd);

    if  (first == null)
        last = newNode;
    else
    {
        first.setPrev(newNode);
        newNode.setNext(first);
    }

    first = newNode;
    size++;
    }


    public void addLast (E toAdd)
    {
    Node newNode = new Node(toAdd);

    if  (last == null)
        first = newNode;
    else
    {
        last.setNext(newNode);
        newNode.setPrev(last);
    }

    last = newNode;
    size++;
    }




    public E find (E tofind)
    {
    return null;
    }

    public OurListIteratorInterface <E> getIteratorAtFirst ()
    {
    return(new OurListIterator<E>(null,true));
    }

    public OurListIteratorInterface <E> getIteratorAtLast ()
    {
    return(new OurListIterator<E>(null,false));
    }

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.