title: Java doubly linked list pointers to previous nodes
body: <p>I'm making a method to add a Node into a list called "public void add(int index, T value)".</p>
  
  <p>This method will put a value into an index, and then will have pointers to the next and previous elements in the list. I have messed up on the pointers to the previous nodes, which I have been sitting and experimenting but don't get to make it work. </p>
  
  <p>Example: We have a list with Integer values [2, 4, 6]
  Instance variables: 
  Node head, tail;
  int amount, changes;</p>
  
  <p>Instance variables for the inner class are:
  T value;
  Node prev, next;</p>
  
  <p>Input:</p>
  
  <pre><code>add(1, 3);
  System.out.println(list.toString());
  System.out.println(list.backwardsString());
  </code></pre>
  
  <p>Expected output: </p>
  
  <pre><code>[2, 3, 4, 6]
  [6, 4, 3, 2]
  </code></pre>
  
  <p>My code so far:</p>
  
  <pre><code>public void add(int index, T value) {
  if (index &lt; 0 || index &gt; amount) {
          throw new IndexOutOfBoundsException("Index is not between 0 and amount!");
      }
  
      if (value== null) {
          throw new NullPointerException("Value can't be null!");
      }
  
      if (amount == 0) {
          head = tail= new Node&lt;&gt;(value, null, null);
      }
      else if (index == 0) {
          head = head.prev= new Node&lt;&gt;(value, null, head);
      }
      else if (index == amount) {
          tail = tail.next= new Node&lt;&gt;(value, tail, null);
      } 
      else {
          Node&lt;T&gt; p = head;  
          for (int i = 1; i &lt; index; i++) {
              p = p.next;
          }
          p.next= new Node&lt;&gt;(value, p, p.next);
  
  
          p = tail;
          for (int i = amount; i &gt; index; i--) {
              p.prev= new Node&lt;&gt;(value, p.prev, p);
              p = p.prev;
          }*/
      }
  
      amount++;
      changes++;
  }
  </code></pre>
  
  <p>In this occasion I would also ask how does</p>
  
  <p>p.prev.next or p.next.prev</p>
  
  <p>work?</p>