Link to home
Start Free TrialLog in
Avatar of theldro
theldro

asked on

Implementing compareTo on linked list

Hey. Im trying to implement the compareTo method in java on a linked list. Not doing a very good job at it so hopeing for some help. Heres some code. Ive never done this so sorry if my questions sound retarted, or my code is way off.
Thanks
public class MutibleString implements Comparable<MutibleString>/*I want to compare 2 MutibleString's so is it best to put that in the<> or just not use these <>*/
{
/* Also do I need any spacific fields to implemtnt the Comparable interface right? these are what I have right now*/
         private Node head;
	private int size; 
	public char index;

/*
in here I have basic methods add remove charAt etc...
....
*/



public boolean compareTo(MutibleString that) 
	{
		if(this.MutibleString.compareTo(that.MutibleString))
		{
			return true;
		}
		return false;	
	}
}

Open in new window

Avatar of ksivananth
ksivananth
Flag of United States of America image

>> if(this.MutibleString.compareTo(that.MutibleString))

actually you need to compare the fields here!

you have three fields head, size, index, you need to decide which field or weight of each field and then compare them! for e.g., if you just want to comapre based on the size,

 return size - that.getSize() ;
ASKER CERTIFIED SOLUTION
Avatar of anilallewar
anilallewar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of theldro
theldro

ASKER

Thanks