944,103 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1190
  • Java RSS
Nov 6th, 2009
0

linked list polynomial help with add Term?

Expand Post »
I am currently building a Polynomial linked list.

Classes

Node, that has a Term and a Node next.
http://www.boomica.com/Node.java

Term with a coefficient and exponent.
http://www.boomica.com/Term.java

Polynomial

http://www.boomica.com/Polynomial.java

Java Syntax (Toggle Plain Text)
  1. public void addTerm(Term nomial)
  2. {
  3. Node temp = first;
  4. Node toAdd;
  5. if(isEmpty() || (temp.getTerm().compareTo(nomial) <= 0))
  6. {
  7. Node newFirst = new Node(nomial, first);
  8. first = newFirst;
  9. }
  10. else
  11. {
  12. while((temp.getTerm().compareTo(nomial) >= 0) && temp.getNext() != null)
  13. {
  14. temp = temp.getNext();
  15. }
  16.  
  17. if(temp.getNext() == null)
  18. {
  19. toAdd = new Node(nomial, null);
  20. temp.setNext(toAdd);
  21. }
  22. else
  23. {
  24. toAdd = new Node(nomial, temp.getNext());
  25. temp.setNext(toAdd);
  26. }
  27. }
  28. }

My Driver:

Java Syntax (Toggle Plain Text)
  1. package dsa.exam;
  2.  
  3. public class PolynomialDriver
  4. {
  5.  
  6. public static void main (String args[])
  7. {
  8. Polynomial nomial = new Polynomial();
  9.  
  10. nomial.addTerm(new Term(3));
  11. nomial.addTerm(new Term(2,7));
  12. nomial.addTerm(new Term(3,6));
  13. nomial.addTerm(new Term(3, 1));
  14. nomial.addTerm(new Term(3, 8));
  15. //nomial.addTerm(new Term(3,2));
  16. //nomial.addTerm(new Term(3,5));
  17. //double answer = one.evaluate(2) + two.evaluate(2) + three.evaluate(2) + four.evaluate(2) + five.evaluate(2);
  18.  
  19. String s = nomial.printPolynomial();
  20.  
  21. System.out.println(s);
  22. }
  23.  
  24. }

It's seems its messing up when I try to switch 3 + 3x(or 3x^1), it wont sort after.

I went through step by step with the Eclipse Debugger and couldnt find it.

Could anyone help me out, and tell me why it isnt sorting correctly and to share opinions on my Add method as do you think it is efficient?

Help is greatly appreciated! =)
Similar Threads
.11
Reputation Points: 10
Solved Threads: 7
Light Poster
.11 is offline Offline
49 posts
since Feb 2009
Nov 8th, 2009
0
Re: linked list polynomial help with add Term?
Found another problem, seems this doesnt encounter for when I have the same terms.

I will need to probably set it up like

Java Syntax (Toggle Plain Text)
  1. if(exponents match)
  2. {
  3. nomial = term.addition(term) //Addition in term class
  4. }
  5.  
  6. Node toAdd(nomial.....etc)

BUt I still cant figure out why it messes up the descending linked list when I add terms with exponent on 0.
.11
Reputation Points: 10
Solved Threads: 7
Light Poster
.11 is offline Offline
49 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need Project Suggestion
Next Thread in Java Forum Timeline: Building a simple Java web crawler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC