Implementing my own Ordered Doubly Linked List (Java)
<p>I know this question's been asked a couple times before, but none of the other topics seem to discuss exactly what I'm trying to do.</p>

<pre><code>public void add(int Value) {
      DListNode previous = null;
      DListNode current = first;

      while ((Integer)current.getValue() &lt; Value) {
          previous = current;           //move previous up to current
           current = current.getNext(); //advance current one node ahead

           if (current == null) {  //if current is the very last node in the list
               break;
           }
      }

      if (previous == null) { //if the previous object is null, the value should be inserted at the front
          first = new DListNode(Value, first, null);
      }
      else { //if not, the value should be inserted between current and previous
          previous.setNext(new DListNode(Value, current, previous));

      }

      getLast();  //updates the last field (Not important)

  }
</code></pre>

<p>DListNode is a class that contains an integer variable, the Next DListNode, and the previous DListNode (along with the standard getter and setter methods).  It's initialized with the arguments DListNode(value, next node, previous node).  The value stored is of type Object.</p>

<p>What I'm trying to do is insert a new node between current and previous. The new node should be set as the next node of previous, with current being set as the next node of the new one, while setting previous as the previous node of the new node, and the new node as the previous node of current.  This should only happen if the value is greater than the value contained in the first node.  However, the nodes only become linked forwards, and I have no idea why.</p>

<p>I can post the entire class if necessary, any help or ideas would be greatly appreciated.</p>

<p>edit: I figured it out with the help of Archer.  In case anyone's wondering, this is my final method (i had to add another if/else statement to deal with nullPointerErrors).</p>

<pre><code>public void add(int Value) {
      DListNode previous = null;
      DListNode current = first;

      while ((Integer)current.getValue() &lt; Value) {
          previous = current;           //move previous up to current
           current = current.getNext(); //advance current one node ahead

           if (current == null) {  //if current is the very last node in the list
               break;
           }
      }

      if (previous == null) { //if the previous object is null, the value should be inserted at the front
          DListNode insert = new DListNode(Value, current, previous);
          current.setPrevious(insert);
          first = insert;
      }
      else { //if not, the value should be inserted between current and previous
          if (current == null) {
          DListNode insert = new DListNode(Value, current, previous);
          previous.setNext(insert);
          }
          else {
             DListNode insert = new DListNode(Value, current, previous);
             current.setPrevious(insert);
              previous.setNext(insert);
          }

      }

      getLast();  //updates the last field

  }
</code></pre>
