java - How to sort a linked list of Polynomials by degree? -
currently i've written method adds 2 polynomials together. poly1 , poly2. logic of method follows, first adds matching degree terms poly1 , poly2, adds non-matching terms poly1, , adds non-matching terms poly2. because of this, terms out of order.
polynomial answer = new polynomial(); (node firstpoly = poly; firstpoly != null; firstpoly = firstpoly.next){ boolean polyadded = false; (node secondpoly = p.poly; secondpoly != null; secondpoly = secondpoly.next){ if (firstpoly.term.degree == secondpoly.term.degree){ answer = addtorear(answer, (firstpoly.term.coeff + secondpoly.term.coeff), firstpoly.term.degree, null); if (answer.poly.term.coeff == 0){ answer.poly = null; } polyadded = true; } } if (polyadded == false){ answer = addtorear(answer, firstpoly.term.coeff, firstpoly.term.degree, null); if (answer.poly.term.coeff == 0){ answer.poly = null; } } } (node secondpoly = p.poly; secondpoly != null; secondpoly = secondpoly.next){ boolean match = false; (node answerpoly = answer.poly; answerpoly != null; answerpoly = answerpoly.next){ if (secondpoly.term.degree == answerpoly.term.degree){ match = true; break; } } if (match == false){ answer = addtorear(answer, secondpoly.term.coeff, secondpoly.term.degree, null); } } return answer; //alt + shift + r }
if code outputs:
8.0x^4 + 4.0x^5 + 2.0x^3 + -1.0x + 12.0
the linked list represented such:
(coefficient, degree) // (12, 0) -> (-1, 1) -> (2, 3) -> (4, 5) -> (8, 4)
i want sort answer polynomial order of degree. linked list should represented this:
(coefficient, degree) // (12, 0) -> (-1, 1) -> (2, 3) -> (8, 4) -> (4, 5)
edit: found solution on own. here sorting method created:
private polynomial sortbydegree(polynomial p){ node prev = p.poly; node current = p.poly.next; while (current != null){ if (current.term.degree < prev.term.degree){ int temp = current.term.degree; current.term.degree = prev.term.degree; prev.term.degree = temp; float temp2 = current.term.coeff; current.term.coeff = prev.term.coeff; prev.term.coeff = temp2; prev = p.poly; current = p.poly.next; } prev = prev.next; current = current.next; } return p; }
thanks everyone!
can suggest 2 approaches:
first. fetch linked list array list (stack), sort array list, rebuild linked list.
second. use following algo:
void sortpoly(polynomial answer) { float lowermargin = 0; node head = null, tail = null; while (answer.poly != null) { node next = detouchmin(lowermargin, answer); lowermargin = next.term.degree; if (tail != null) { tail.next = next; tail = next; else { head = next; tail = next; } } answer.poly = head; } node detouchmin(float lowermargin, polynomial answer) { float min = float.inf; node n = null; node t = answer.poly; while (t != null) { if ((t.term.degree > lowermargin) && (t.term.degree < min)) { n = t; min = n.term.degree; } t = t.next; } if (n != null) { node t = answer.poly, prev = null; while (t != null) { if (t == n) { if (prev != null) answer.poly = t.next; else prev.next = t.next; } prev = t; t = t.next; } } return n; }
note: code not tested
Comments
Post a Comment