4 Replies Latest reply: Mar 22, 2008 4:05 PM by 807601 RSS

    doubly linked list

    807601
      Hello, I have a problem compiling this. (see details after the code)
      public class XList<T>
      {     
           private int length = 0;
           private Nodes<T> head = null;
      
           public XList()
           {
                   head = null;
           }
      
          public T get(int index)
                  throws java.util.NoSuchElementException
          {
             if(index < 0 || index > length)
             {
                 throw new java.util.NoSuchElementException();
             }
             else
             {
                 Nodes<T> tracker = head;
                 for(int i = 0; i < index; i++)
                 {
                     tracker = tracker.next;
                 }
                 return tracker;
             }
          }
      
      
      
      
          private static class Nodes<T>
          {
              public T data;
              public Nodes<T> next, prev;
              
              public Nodes(T val, Nodes<T> newPrev)
              {
                  data = val;
                  prev = newPrev;
              }
              
              public Nodes(T val)
              {
                  data = val;
                  prev = null;
              }
          }
      Okay, so I'm getting an error when compiling this. The T identifier is suppose to symbolize any of the data types depending on the input (string, int, double, etc.). I think it's because my get method has a return type T and my tracker variable takes in the Node<T>. I tried casting tracker to T but it gave me a weird output. Also, can someone confirm if my code is correct?

      P.S. Don't mind the length variable, i'm creating another method that tracks down the length of the list. :)

      Thanks.

      Edited by: kreuzen on Mar 21, 2008 10:23 PM
        • 1. Re: doubly linked list
          807601
          um... you probably want to return tracker.data instead
          • 2. Re: doubly linked list
            807601
            Oh geez, i'm completely stupid. Thank you.
            • 3. Re: doubly linked list
              807601
              Okay, so I got everything to compile. Although, when I tried printing it out to a string, I keep getting a NullPointerException. This is what I have so far:
              public class XList<T>
              {     
                   private int length = 0;
                   private Nodes<T> head;
                   private Nodes<T> tail;
              
                  public XList()
                  {
                      head = null;
                      tail = null;
                  }
                  
                  public int size()
                  {
                      return length;
                  }
                  
                  public T get(int index)
                          throws java.util.NoSuchElementException
                  {
                     if(index < 0 || index > length)
                     {
                         throw new java.util.NoSuchElementException();
                     }
                     else
                     {
                         Nodes<T> tracker = head;
                         for(int i = 0; i < index; i++)
                         {
                             tracker = tracker.next;
                         }
                         return tracker.data;
                     }
                  }
              
                  public int add(T val)
                  {
                      Nodes newLink = new Nodes(val);
                      if(head == null)
                      {
                          head = newLink;
                          length++;
                      }
                      else
                      {
                          tail.next = newLink;
                          newLink.prev = tail;
                          length++;
                      }
                      tail = newLink;
                      
                      return length;
                  }
                  
                  
                  public String toString()
                  {
                      if(length == 0)
                      {
                          return "[  ]";
                      }
                      
                      Nodes<T> current = head.next;
                      String str = "[ " + current.data;
                      
                      while(current.next != head)
                      {
                          current = current.next;
                          str += ", " + current.data;
                      }
                      
                      str += " ]";
                      return str;
                  }
                  
                  
                  
                  
                  
                  
                  
                  
                  private static class Nodes<T>
                  {
                      public T data;
                      public Nodes<T> next;
                      public Nodes<T> prev;
                      
                      public Nodes(T val)
                      {
                          data = val;
                      }
                  }
              }
              Anyone care to point out what i'm doing wrong? Thanks.

              Edited by: kreuzen on Mar 22, 2008 1:21 PM

              Edited by: kreuzen on Mar 22, 2008 1:22 PM
              • 4. Re: doubly linked list
                807601
                the error seems to point at this line:
                String str = "[ " + current.data;
                i'm not exactly sure why that's causing a NullPointerException though. Anyone care to shed some light on this issue?

                Edited by: kreuzen on Mar 22, 2008 2:05 PM