title: Circular Double Linked List Infinite Loop
body: <p>My CS professor asked us to develop our own Java program using circular linked lists. My project is to add or delete names (of type String) from a circular list. So far, my add methods work perfectly; however, my removeNode() method does not work and does not remove the desired element. It also goes on an infinite loop and I have tried so many pieces of code and neither of them work. 
  My remove method is the following: </p>
  
  <pre><code>public E removeNode(E nodeToBeDeleted)
  {
      Node&lt;E&gt; nodeFound = findNode(nodeToBeDeleted);
  
      if(nodeFound != null)
      {
          nodeFound.prev.next = nodeFound.next;
          nodeFound.next.prev = nodeFound.prev;
          size--;
          return nodeFound.data;  
      }
      return null;
  }
  </code></pre>
  
  <p>basically, the findNode() searches for the node whose data is equal to the String plugged in as a parameter, but when I call the outputList() method, which returns a String representation of the current nodes on screen, it goes on infinite loop.</p>
  
  <p>The outputList method is:</p>
  
  <pre><code>public void outputList()
  {   
      Node&lt;E&gt; position = head;
      do 
      {
          System.out.print(position.data + " ==&gt; ");
          position = position.next;
  
      } while((position != null) &amp;&amp; (position.next != position));
  }
  </code></pre>
  
  <p>Any help would be highly appreciated.. Thanks in advance.</p>
  
  <p>The Node class is:</p>
  
  <pre><code>    static class Node&lt;E&gt; {
  
      /** The data value. */
      private E data;
      /** The link to the next node. */
      private Node&lt;E&gt; next = null;
      /** The link to the previous node. */
      private Node&lt;E&gt; prev = null;
  
      private Node(E dataItem) {
          data = dataItem;
      }
  
  
      private Node(E newData, Node&lt;E&gt; nodeRef)
      {
          data = newData;
          next = nodeRef;
      }
  
      private Node(Node&lt;E&gt; prevRef, E newData)
      {
          data = newData;
          prev = prevRef;
      }
  
     //set next link
      private Node(Node&lt;E&gt; newData, Node&lt;E&gt; nodeRef)
      {
          data = (E) newData;
          next = nodeRef;
      }
  } //end class Node
  </code></pre>