I am attempting to add multiple polynomials which are entered in sparse input/output format. Basically to represent 1 + 5x + 2x^2 the input would be
0,1:1,5:2,2;
;
If I wished to add 3x^2 the input would be
2,3;
;
The power is always ordered from smallest to largest and 0 constants are not printed. I have not used recursive functions and my algorithm is working for "small" (20 milliseconds) and "medium" (50 milliseconds) input sizes. However when I test for "large" input sizes, I just get a stack overflow error! Below is my code,
Thanks in advance!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class list {
//contains the reference to the first node in the list
private node firstNode;
//construct an empty list called linkedList
public list( String linkedList) {
firstNode = null;
}
public list () {
this("linkedlist");
}
public String toString(){
return firstNode +"";
}
//where i is the power
public node find (int power, int coeff ) {
node nodePos = null;
node n = firstNode;
while ( n!= null ) {
if (n.pow == power) {
nodePos = n;
break;
} else if (n.next == null) {
nodePos = n;
break;
} else if ((n.pow < power) & (n.next.pow > power)){
nodePos = n;
break;
} else if (n.pow > power) {
nodePos = firstNode;
break;
} else {
n = n.next;
}
}
return nodePos;
}
public void insert ( int p, int c) {
node position = null;
//if list is empty, add node as the first element
if ( isEmpty ()) {
firstNode = new node(p, c, null);
} else {
position = find(p, c);
//do addition
if (position.pow == p) {
position.coeff = position.coeff + c;
} else if (position.pow > p) {
//insert before
firstNode = new node (p, c, position);
} else {
//insert after
position.next = new node(p, c, position.next);
}
}
}
private boolean isEmpty() {
return firstNode == null;
}
}
class node {
int pow;
int coeff;
node next;
public node () {
pow = 0;
coeff = 0;
next = null;
}
public node (int pow, int coeff) {
this.pow = pow;
this.coeff = coeff;
next = null;
}
public node (int pow, int coeff, node next) {
this.pow = pow;
this.coeff = coeff;
this.next = next;
}
public String toString(){
if (next == null){
if (coeff == 0) {
return "";
}
return pow+","+coeff+";";
} else if (coeff == 0){
return next + "";
}
else {
return pow+","+coeff+";"+next;
}
}
}
public class Adderv2 {
public static void main(String[]args) throws IOException{
//LinkedList<Integer> list = new LinkedList<Integer>();
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
list linkedList = new list();
while (!line.equals(";")) {
//Split string by ';'
StringTokenizer st = new StringTokenizer(line, ";");
while (st.hasMoreElements()) {
//split string up by ','s
StringTokenizer st2 = new StringTokenizer(st.nextToken(), ",");
while(st2.hasMoreElements()) {
//convert tokens to integer
int i1 = Integer.parseInt(st2.nextToken());
int i2 = Integer.parseInt(st2.nextToken());
//insert the integers into the list
linkedList.insert (i1, i2);
}
}
//read next line
line = br.readLine();
}
System.out.println(linkedList);
System.out.println(";");
}
}
toString, by the way... that may be the problem. Consider using an iterative approach instead, ideally using aStringBuilder...)toStringmethod is probably the culprit, and it probably is just the sheer number of terms that is causing the problem. Using iteration rather than recursion intoString, as Jon suggested, will almost certainly fix this.