generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLPolynomialAdd.java
96 lines (85 loc) · 2.34 KB
/
LLPolynomialAdd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.*;
class node{
public int coeff;
public int exp;
public node next;
public node(int coeff,int exp){
this.coeff = coeff;
this.exp = exp;
next = null;
}
}
class Polynomial{
node first;
public Polynomial(){
first = null;
}
public void addTerm(int coeff, int exp){
node nl = new node(coeff,exp);
if(first == null){
first = nl;
nl.next = null;
}
else{
node temp = first;
while(temp.next != null){
temp = temp.next;
}
temp.next = nl;
}
}
public Polynomial addPolynomial(Polynomial pol1, Polynomial pol2){
Polynomial result = new Polynomial();
node temp1 = pol1.first;
node temp2 = pol2.first;
while(temp1 != null || temp2 != null){
if(temp1.exp > temp2.exp){
result.addTerm(temp1.coeff, temp1.exp);
temp1 = temp1.next;
}
else if(temp2.exp > temp1.exp){
result.addTerm(temp2.coeff, temp2.exp);
temp2 = temp2.next;
}
else{
result.addTerm((temp1.coeff + temp2.coeff), (temp1.exp));
temp1 = temp1.next;
temp2 = temp2.next;
}
}
while (temp1 != null) {
result.addTerm(temp1.coeff, temp1.exp);
temp1 = temp1.next;
}
while (temp2 != null) {
result.addTerm(temp2.coeff, temp2.exp);
temp2 = temp2.next;
}
return result;
}
public void display(){
node current = first;
while (current != null) {
System.out.print(current.coeff + "x^" + current.exp);
if (current.next != null) {
System.out.print(" + ");
}
current = current.next;
}
System.out.println();
}
}
public class LLPolynomialAdd{
public static void main(String args[]){
Polynomial p1 = new Polynomial();
p1.addTerm(3, 2);
p1.addTerm(4, 1);
p1.addTerm(2, 0);
Polynomial p2 = new Polynomial();
p2.addTerm(1, 3);
p2.addTerm(2, 2);
p2.addTerm(5, 0);
Polynomial sum = p1.addPolynomial(p1, p2);
sum.display();
}
}