2 Replies - 3610 Views - Last Post: 03 March 2011 - 10:51 PM Rate Topic: -----

#1 lostintime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 11

Problem with toString() method in a java double circular linked list

Posted 03 March 2011 - 10:23 PM

I'm doing a double circular linked list in java. The toString method I have created seems to be able to only store one item and after that it just gives me a null pointer error.

public String toString(){
  String ret=" ";
  DLNode curr=head;
  int i=1;
  for(i=1,curr=head;i<=size;curr=curr.next,i++){
    ret += curr.data.toString()+ " ";
  }
  
return ret;
}



I've tried a bunch of different things but nothing seems to be working well. Any help would be appreciated.

This post has been edited by pbl: 03 March 2011 - 10:39 PM
Reason for edit:: Code tags fixed


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with toString() method in a java double circular linked list

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Problem with toString() method in a java double circular linked list

Posted 03 March 2011 - 10:42 PM

public String toString(){
  String ret=" ";
  DLNode curr=head;   // no need to init curr here
  int i=1;            // and i here
  // if you re-initialzed them here
  for(i=1,curr=head;i<=size;curr=curr.next,i++){
    ret += curr.data.toString()+ " ";
  }
  
return ret;
}



and I'll bet that your for should be

for(i=0, curr=head; i<size; curr=curr.next, i++){
Was This Post Helpful? 0
  • +
  • -

#3 lostintime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 11

Re: Problem with toString() method in a java double circular linked list

Posted 03 March 2011 - 10:51 PM

I actually found that it was a problem with my add method that was causing the null pointer error. I managed to fix it, but when it adds onto a list greater than two it for some reason decides to list the second added element twice. For instance I did

list.add("one")
list.add("two")
list.add("three")
list.add("four")

and recieved an output of (one two four two), when I put in the first three values I recieved a (one two three). I'm not sure why it is happening.

public boolean add(Object o){
   DLNode curr=new DLNode(o);
  if(o==null){
    System.out.println("False");
    return false;
  }
  
  else if(size==0){
   head=curr;
   head.next=head.prev;
    size++;
    return true;
  }
   else if(size==1){
     DLNode curr2= new DLNode(o);
      head.next=curr2;
      head.prev=curr2;
      curr2.next=head;
      curr2.prev=head;
      size++;
      System.out.println(curr2.data);
      return true;
      }
   else if(size>=2){
   DLNode curr3= new DLNode(o);
   head.prev.next=curr3;
   curr3.next=head.prev;
   curr3.prev=head.prev.prev;
   
  size++;
  
  return true;
   }
  return true;
  }



You will seriously have to learn how to use code tags :code:

This post has been edited by pbl: 03 March 2011 - 10:59 PM
Reason for edit:: Code tags fixed

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1