java - Polynomial Linked List does not return the list -



java - Polynomial Linked List does not return the list -

this first time asking in here. creating polynomial using linked list in java.

i'm trying convert string of polynomial nodes in linked list , add-on , subtraction.

i create method split polynomial terms , store in linked list. tried print out list within method, , works. however, when want print list using tostring method or phone call add together or subtract method, says list null. can help me, , explain me why list go null?

this term class

public class term { int coef; int exp; term next; public term() { coef = 0; exp = 0; next = null; } public term(int coef, int exp) { this.coef = coef; this.exp = exp; this.next = null; } public void setcoef(int coef) { this.coef = coef; } public void setexp(int exp) { this.exp = exp; } public int getcoef() { homecoming coef; } public int getexp() { homecoming exp; } public void setnext(term next) { this.next = next; } public term getnext() { homecoming next; } public string tostring() { string str = ""; if (exp == 0) { if (coef < 0) str += coef; else str += "+" + coef; } else if (exp == 1) { if (coef < 0) str += coef + "x"; else str += "+" + coef + "x"; } else if (coef < 0) str += coef + "x^" + exp; else str += "+" + coef + "x^" + exp; homecoming str; } }

my linkedlistpolynomial class

public class linkedlistpolynomial implements polynomialinterface { int coefficient, exponent; private term head; public linkedlistpolynomial() { head = null; } public linkedlistpolynomial(int coefficient, int exponent) { this.coefficient = coefficient; this.exponent = exponent; head = new term(coefficient, exponent); } public linkedlistpolynomial(string n) { string s1new = n.replaceall("-", "+-"); string[] arr = s1new.split("\\+"); //if first term contains negative coefficient, first term empty if (arr[0].isempty()) { (int = 1; < arr.length; i++) { if (arr[i].contains("x^")) { string str = arr[i].substring(0, arr[i].indexof("x^")); string poly = arr[i].substring(arr[i].indexof("x^") + 2); coefficient = integer.parseint(str); exponent = integer.parseint(poly); if (head == null) { head = new term(coefficient, exponent); } else { term newnode = new term(coefficient, exponent); newnode.setnext(head); head = newnode; } } else { coefficient = integer.parseint(arr[i]); exponent = 0; if (head == null) { head = new term(coefficient, exponent); } else { term newnode = new term(coefficient, exponent); newnode.setnext(head); head = newnode; } } } } else { (int = 0; < arr.length; i++) { if (arr[i].contains("x^")) { string str = arr[i].substring(0, arr[i].indexof("x^")); string poly = arr[i].substring(arr[i].indexof("x^") + 2); coefficient = integer.parseint(str); exponent = integer.parseint(poly); if (head == null) { head = new term(coefficient, exponent); } else { term newnode = new term(coefficient, exponent); newnode.setnext(head); head = newnode; } } else { coefficient = integer.parseint(arr[i]); exponent = 0; if (head == null) { head = new term(coefficient, exponent); } else { term newnode = new term(coefficient, exponent); newnode.setnext(head); head = newnode; } } } } selectionsort(); int = 0; while (head != null) { system.out.print(head); head = head.getnext(); i++; } system.out.println("\n"); } public polynomialinterface add(polynomialinterface other) { linkedlistpolynomial sum = new linkedlistpolynomial(); linkedlistpolynomial parameter = (linkedlistpolynomial) other; homecoming sum; } public polynomialinterface subtract(polynomialinterface other) { linkedlistpolynomial subtract = new linkedlistpolynomial(); linkedlistpolynomial parameter = (linkedlistpolynomial) other; homecoming subtract; } public string tostring() { string str = ""; term current = head; if (current == null) str += "it's null"; else { while (current.getnext() != null) { current = current.getnext(); str += current.getcoef() + "x^" + current.getexp(); } } homecoming str; } public void selectionsort() { (term node1 = head; node1 != null; node1 = node1.getnext()) // number of iterations { term min = node1;// assumes min node node under // considerations // selects min node (term node2 = node1; node2 != null; node2 = node2.getnext()) { if (min.getexp() < node2.getexp()) { min = node2; } } // swaps min node node in actual position term temp = new term(node1.getcoef(), node1.getexp()); node1.coef = min.getcoef(); node1.exp = min.getexp(); min.coef = temp.getcoef(); min.exp = temp.getexp(); } } }

my polynomialinterface

public interface polynomialinterface { polynomialinterface add(polynomialinterface other); polynomialinterface subtract(polynomialinterface other); string tostring(); }

in add , subtract methods creating list using default constructor, sets head null , nil else, returning empty list. you'll need implement functionality of add , subtract. see how implement linked list in java? decent primer.

java

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -