compareTo with linked list.

Hey, Im trying to figure out how to get my compareTo method to work right for my circular doubly linked list. The one I have works fine when I use it on my bubbleSort() method, but it wont work on any other sort I try. The sorts Ive tried are selectionSort(), selectionSortNoSelfSwaping(), and insertionSort().  I get stuck in an infinit loop when I hit the compareTo part of those 3 sorts I mentioned. Well here is the compareTo method im using.
If you need more info about my program ill be on for at least 5 or so hours and can repost more code.
Thanks

public class DLL implements Comparable<Node>//Implementing the comparable interface
{
Node head;
int size;

//all the normal things u have with a circular doubly linked list with dummy head. 
// also have the Node class with all the basic things.


public int compareTo(Node that) 
{		      				
return ((Comparable)this.head).compareTo((Comparable)that.data);
}	

}//end DLL

//The Node class has fields of

Node next;
Node prev;
Object data

Open in new window

theldroAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
> public class DLL implements Comparable//Implementing the comparable interface

the Node class should implement Comparable, not the DLL class
0
theldroAuthor Commented:
I put the almost the same code in both of them because I wasnt sure which one was supposed to implement it. This is what it looks like in the Node class.


public class Node implements Comparable<Node>
{
Node next;
Node prev;
Object data;

public int compareTo(Node that) 
{ 
return ((Comparable)data).compareTo((Comparable)that.data);		             
}
}

Open in new window

0
Mick BarryJava DeveloperCommented:
thats looks ok
The DLL class does not need to implement Comparable for you to be able to compare nodes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
theldroAuthor Commented:
If the compareTo is ok than I must have 3 bad sorting routines.  I  thought  they were good since they all got into the infinate loop at the compareTo spot. Ill post 2 of them.
public void insertionSort()
			{  Node back = head;

			   if (size < 2)
			      return;
			   back = back.next;           // SECOND entry in the list
			   while ( back != null )      // I.e., end-of-list
			   {  Comparable value = back.data;
			      Node curr = head;        // Start at the front
			      
			      // Find insertion point for value;
			      while (curr != back && value.compareTo(curr.data) >= 0)
			         curr = curr.next;
			      // Propogate values upward, inserting the value from back
			      while (curr != back)
			      {  
			    	 Comparable hold = curr.data;
			         curr.data = value;
			         value = hold;
			         curr = curr.next;
			      }
			      back.data = value;      // Drop final value into place!
			      back = back.next;       // Move sorted boundary up
			   }
			} // end insertSort()





public void selectSort()
{  Node front = head;

   // Nothing to do on an empty list
      if ( front == null )
         return;
      while ( front.next != null )     // skips a one-entry list
      {
         Node       tiny = front,
                    curr = front.next;
         Comparable temp = front.data; // start the swap

         for ( ; curr != null ; curr = curr.next )
         {  if ( tiny.data.compareTo(curr.data) > 0 )
               tiny = curr;
         }
         front.data = tiny.data;       // Finish the swap
         tiny.data = temp;
         front = front.next;           // Advance to the next node
      }
      // The structure is unchanged, so the validity of tail is unchanged.
   }
}

Open in new window

0
theldroAuthor Commented:
Thanks for the help with all the questions ive had.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.