2 Replies - 218 Views - Last Post: 29 October 2017 - 10:58 AM Rate Topic: -----

#1 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

linked list polynomial equals.method

Posted 29 October 2017 - 12:56 AM

I'm trying to write an equals method for a polynomial linked list that holds 3 terms (coefficient, exponent, and link). I keep getting it saying "false" or "True" when it should be the other way around, this is what I have:

public boolean equals (Object obj) 
   {
   if(obj instanceof Polynomial)
   {
     Polynomial candidate = (Polynomial) obj;
     TermNode cursor1 = head;
     TermNode cursor2 = candidate.head ;
     
     while(cursor1 != null)
     {  
      while(cursor2 != null)
      {
      if(!((cursor1.coefficient == cursor2.coefficient) || (cursor1.exponent==cursor2.exponent)))
       {
       return false;
       }
      cursor1 = cursor1.link;
      cursor2 = cursor2.link;
      }
     }
      return true;
   }
   return false;
} 


I'm basically testing it in the main method with a simple polynomial like p1 = 7x^7 + 6 and p2 = 6x^7 + 6 and it should be false but reads true and blah blah blah

I've tried editing it and writing it like 30 other ways (i.e. using while ((cursor1 != null) && (cursor 2 != null))) etc but to no avail

Is This A Good Question/Topic? 0
  • +

Replies To: linked list polynomial equals.method

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: linked list polynomial equals.method

Posted 29 October 2017 - 04:45 AM

Well, i'll scramble things a little and say i think (i don't know your class - you haven't helped by showing out-of-context code) it's not correct - even if it 'worked' ;) Why? Because, if it 'worked' it would return true if one list were a truncated form of the other. Try again and count the nodes.
Was This Post Helpful? 0
  • +
  • -

#3 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

Re: linked list polynomial equals.method

Posted 29 October 2017 - 10:58 AM

This is a data structures Java class.

I have also been testing same size polynomials!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1