insertion sort on linked list -


//i wrote java code insertion method on doubly linked list there infinite loop //when run it. i'm trying find bug, have not found far. suggestions? //it calling helper function

 public intlist insertionsort ( ) {     dlistnode sofar = null;     (dlistnode p=myhead; p!=null; p=p.mynext) {         sofar = insert (p, sofar);     }     return new intlist (sofar); }   // values in decreasing order. private dlistnode insert (dlistnode p, dlistnode head) {     dlistnode q=new dlistnode(p.myitem);     if(head==null){         head=q;         return head;     }     if(q.myitem>=head.myitem){         dlistnode te=head;         q.mynext=te;         te.myprev=q;         q=head;         return head;     }     dlistnode a;     boolean found=false;     for(a=head; a!=null;){         if(a.myitem<q.myitem){             found=true;             break;         }         else{             a=a.mynext;         } }     if(found==false){         dlistnode temp=mytail;         temp.mynext=q;         q.myprev=temp;         mytail=q;         return head;     }     if(found==true){     dlistnode t;     t=a.myprev;     a.myprev=q;     t.mynext=q;     q.myprev=t;     q.mynext=a; }     return head; 

}

your code bit hard read through noticed few problems

first: handling case inserting number @ head of list:

if(q.myitem>=head.myitem){         dlistnode te=head;         q.mynext=te;         te.myprev=q;         q=head;         return head;     } 

specifically line q=head; , return. q=head can removed, , should return q not head because q new head. think meant head=q; return head;. current code add new node on front never return updated head "fall off edge" in way.

second: assuming mytail node reference keeping myhead original list. don't think want using sorted list constructing. when loop through looking place insert in new list, use determine tail reference , use instead.

dlistnode lastcompared = null; for(a=head; a!=null; a=a.mynext) {     lastcompared = a;     if(a.myitem<q.myitem) {         break;         }     } if( )     {     // insert node before     ...     } else     {     // smallest value yet, throw on end     lastcompared.mynext = q;     q.myprev = lastcompared;     return head;     } 

finally make sure myprev , mynext being initialized null in constructor dlistnode.

disclaimer didn't chance test code added here, @ least gets thinking solution.


a couple stylistic notes (just sidenote):

  1. the repeated if->return format not cleanest in opinion. try , limit exit points in functions
  2. there lot of intermediate variables being used , names super ambiguous. @ least try , use more descriptive variable names.
  3. comments idea. make sure don't explain code doing - instead try , convey thought process , trying accomplished.

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -