title: Insertion in a doubly circular linked list in java
body: <p>For homework I am supposed to create a circularly linked list using nodes and pointers.<br>
  This is my node class</p>
  
  <pre><code>class Node implements Serializable {
      public String theName; //the wrapped name
      public Node next;      //the next node in the sequence
      public Node prev;      //the previous node in the sequence
  
      public Node(Node p, String s, Node n){
          prev = p;
          theName = s;
          next = n;
      }
  }
  </code></pre>
  
  <p>I am trying to insert strings (names) at the front of the list and then print them out using the traverse method.<br>
  So far these are my traverse and insert methods...</p>
  
  <pre><code>public class ListImpl /*implements List*/ {
      Node list;
  
      public ListImpl() {
          list = new Node(list, null, list);
      }
  
      public void insert(String s) {
  
  
          if (list == null) {
              list = new Node(list, s, list);
          } else {
              list = new Node(list.prev, s, list);
              list.next.prev = list;
              list.next.next = list;
  
  
  
          }
      }
  
      public void traverse(ASCIIDisplayer out) {
  
          Node p = new Node(null, null, null);
          p = list;
          if (list != null) {
  
              while(true) {
                  out.writeString(p.theName);
                  p = p.next;
              }
          } else {
              throw new EmptyListException();
          }
      }
  }
  </code></pre>
  
  <p>I understand that my traverse method will me a continuous loop and I have to figure out how to make it stop when it gets back to the beginning of the list, but that is not my problem.<br>
  I believe my problem lies in the insert method because my output is not as expected when I run this code (the main):</p>
  
  <pre><code>public class Main {
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
  
          ASCIIDisplayer a = new ASCIIDisplayer();
          ListImpl List;
  
          List = new ListImpl();
          List.insert("Steve");
          List.insert("Kuba");
          List.insert("Taylor");
          List.insert("Jane");
  
          List.traverse(a);
      }
  }
  </code></pre>
  
  <p>The output I get is: Taylor Jane Taylor Jane Taylor Jane Taylor Jane... repeated.<br>
  I expected the output to be: Steve Kuba Taylor Jane Steve Kuba Taylor Jane...repeated.</p>
  
  <p>This is why I think that the problem is in the insert method, my pointer must be pointing to the wrong nodes but I just can't figure out what I did wrong.</p>
  
  <p>Sorry for the long question, hopefully there is enough information for you to help me! 
  Thanks in advance!</p>