title: Java Doubly Linked List last node won't point to list properly
body: <p>I want to add a node to the end of my list, but I can't seem to connect the link between my first node and where it ends.</p>
  
  <p>I know my logic can't be right because the only way I can sort of get it to work requires that I overwrite last and assign that to null. If I don't do this, the program goes into an infinite loop because my guess is that there isn't a node that = null to stop a loop from ending. I figured line 20 would create that new object to hold null, and then line 21 would link temp back to it.</p>
  
  <pre><code>public class LinkedListDeque {
  
  public DoubleNode first = new DoubleNode();
  public DoubleNode last = new DoubleNode();
  public DoubleNode temp;
  public int N;
  
  
  private class DoubleNode {
  
      String item;
      int counter = 0;
      DoubleNode next;
      DoubleNode prev;
      DoubleNode last;
  
      DoubleNode() {
      }
  
      DoubleNode(String i) {
          this.item = i;
      }
  
  }
  
  
  public void enqueue(String item) {
  
      temp = getNode(null); //returns pointer to last node in "first"
  
      System.out.println("\nenqueue\n***********");
  
      DoubleNode oldlast;        
  
      oldlast = temp;
      temp.item = item;
  
      last = temp;
  
      System.out.println("last = " + last.item);  // = last item
      System.out.println("temp = " + temp.item);  // = last item    
  
  line 20     temp = new DoubleNode();    
  line 21     temp = oldlast;    
  
      DoubleNode last;              //will go into infinite loop w/out 
      last = temp;                  //these two lines
  
      System.out.println("last = " + last.item);   // = null
      System.out.println("temp = " + temp.item);   // = null
  
      if (isEmpty()) {            //returns true if first == null
          first = last;
      } else {
          oldlast.next = last;
  
      }
      N++;
   }
  
  }
  </code></pre>