linked list polynomial help with add Term?

Reply

Join Date: Feb 2009
Posts: 37
Reputation: .11 is an unknown quantity at this point 
Solved Threads: 6
.11 .11 is offline Offline
Light Poster

linked list polynomial help with add Term?

 
0
  #1
20 Days Ago
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

  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:

  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! =)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 37
Reputation: .11 is an unknown quantity at this point 
Solved Threads: 6
.11 .11 is offline Offline
Light Poster
 
0
  #2
18 Days Ago
Found another problem, seems this doesnt encounter for when I have the same terms.

I will need to probably set it up like

  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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC