-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgLLpolynomial.cpp
More file actions
183 lines (162 loc) · 4.4 KB
/
sgLLpolynomial.cpp
File metadata and controls
183 lines (162 loc) · 4.4 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//LL of polynomial and perform operations in it
#include<iostream>
#include<string>
#define null 0
using namespace std;
struct polynode
{
int coef,pow;
polynode *next;
};
polynode *first,*temp,*ttemp,*poly1,*poly2,*sum,*diff,*prod,*a,*b;
void init()
{
first=temp=ttemp=poly1=poly2=null;
}
void insertdata(polynode*& head, int coef, int pow)
{
// Skip inserting zero coefficient terms
if (coef == 0) return;
ttemp = new polynode{coef, pow, nullptr};
// Insert at head if list empty or new pow is greater than head pow (descending order)
if (!head || pow > head->pow)
{
ttemp->next = head;
head = ttemp;
return;
}
temp = head;
while (temp)
{
// If same power, accumulate coefficient and delete new node
if (temp->pow == pow)
{
temp->coef += coef;
// If coefficient becomes zero, remove the node
if (temp->coef == 0)
{
// remove head
if (temp == head)
{
polynode* toDel = head;
head = head->next;
delete toDel;
} else {
// find previous to temp
polynode* prev = head;
while (prev && prev->next != temp) prev = prev->next;
if (prev) {
prev->next = temp->next;
delete temp;
}
}
}
delete ttemp;
return;
}
if (!temp->next || temp->next->pow < pow)
break;
temp = temp->next;
}
ttemp->next = temp->next;
temp->next = ttemp;
}
void disp(polynode* head)
{
while (head) {
cout << head->coef << "x^" << head->pow;
if (head->next) cout << " + ";
head = head->next;
}
cout << "\n";
}
polynode* addPoly(polynode*p1,polynode*p2)
{
polynode* result = null;
// Merge-like safe approach: process while both non-null, then append remaining
while (p1 && p2) {
if (p1->pow > p2->pow) {
insertdata(result, p1->coef, p1->pow);
p1 = p1->next;
} else if (p2->pow > p1->pow) {
insertdata(result, p2->coef, p2->pow);
p2 = p2->next;
} else { // equal powers
insertdata(result, p1->coef + p2->coef, p1->pow);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) { insertdata(result, p1->coef, p1->pow); p1 = p1->next; }
while (p2) { insertdata(result, p2->coef, p2->pow); p2 = p2->next; }
return result;
}
polynode* subPoly(polynode*p1,polynode*p2)
{
polynode* result = null;
while (p1 && p2) {
if (p1->pow > p2->pow) {
insertdata(result, p1->coef, p1->pow);
p1 = p1->next;
} else if (p2->pow > p1->pow) {
insertdata(result, -p2->coef, p2->pow);
p2 = p2->next;
} else {
insertdata(result, p1->coef - p2->coef, p1->pow);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) { insertdata(result, p1->coef, p1->pow); p1 = p1->next; }
while (p2) { insertdata(result, -p2->coef, p2->pow); p2 = p2->next; }
return result;
}
polynode* mulPoly(polynode*p1,polynode*p2)
{
polynode* result = null;
for ( a = p1; a != null; a = a->next) {
for (b = p2; b != null; b = b->next) {
int newCoef = a->coef * b->coef;
int newPow = a->pow + b->pow;
insertdata(result, newCoef, newPow);
}
}
return result;
}
int main()
{ init();
int n,coef,pow;
//insert poly1
cout<<"enter no. of terms in poly1: ";
cin>>n;
cout<<"\nenter terms as coef and pow\n";
for (int i = 0; i < n; i++) {
cin >> coef >> pow;
insertdata(poly1, coef, pow);
}
//insert poly2
cout<<"enter no. of terms in poly2: ";
cin>>n;
cout<<"\nenter terms as coef and pow\n";
for (int i = 0; i < n; i++) {
cin >> coef >> pow;
insertdata(poly2, coef, pow);
}
//display original
cout << "\nPolynomial 1: ";
disp(poly1);
cout << "Polynomial 2: ";
disp(poly2);
// Perform operations
sum = addPoly(poly1, poly2);
diff = subPoly(poly1, poly2);
prod = mulPoly(poly1, poly2);
// Display results
cout << "\nSum: ";
disp(sum);
cout << "Difference: ";
disp(diff);
cout << "Product: ";
disp(prod);
return 0;
}