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
public void addTerm(Term nomial)
{
Node temp = first;
Node toAdd;
if(isEmpty() || (temp.getTerm().compareTo(nomial) <= 0))
{
Node newFirst = new Node(nomial, first);
first = newFirst;
}
else
{
while((temp.getTerm().compareTo(nomial) >= 0) && temp.getNext() != null)
{
temp = temp.getNext();
}
if(temp.getNext() == null)
{
toAdd = new Node(nomial, null);
temp.setNext(toAdd);
}
else
{
toAdd = new Node(nomial, temp.getNext());
temp.setNext(toAdd);
}
}
}
My Driver:
package dsa.exam;
public class PolynomialDriver
{
public static void main (String args[])
{
Polynomial nomial = new Polynomial();
nomial.addTerm(new Term(3));
nomial.addTerm(new Term(2,7));
nomial.addTerm(new Term(3,6));
nomial.addTerm(new Term(3, 1));
nomial.addTerm(new Term(3, 8));
//nomial.addTerm(new Term(3,2));
//nomial.addTerm(new Term(3,5));
//double answer = one.evaluate(2) + two.evaluate(2) + three.evaluate(2) + four.evaluate(2) + five.evaluate(2);
String s = nomial.printPolynomial();
System.out.println(s);
}
}
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! =)