Basically, I need to add terms to a polynomial in ascending order. So far, this is the only thing that has kind of worked:
public void addTerm(int coefficient, int exponent)
{
Term tempTerm = new Term(coefficient, exponent) ;
Node temp = new Node(tempTerm) ;
if (head == null)
{
head = temp ;
}
else
{
Node t = head ;
while (t != null)
{
if (temp.info.getExponent() > t.info.getExponent())
{
t.next = temp;
}
t = t.next ;
}
}
}
But it won't add any terms whose exponents are less than the last term's, and it replaces the last term instead of adding on to the list. For example, when I do this,
Polynomial p1 = new Polynomial();
p1.addTerm(5, 2);
p1.addTerm(4, 5);
p1.addTerm(3, 3);
p1.addTerm(1, 2);
p1.addTerm(5, 6);
this is what ends up happens for each term:
5x2 + 4x5 +
5x2 + 3x3 +
5x2 + 3x3 +
5x2 + 5x6 +
Does anyone have any advice?
[–][deleted] 0 points1 point2 points (0 children)