-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolymulti_ll.c
276 lines (260 loc) · 5.81 KB
/
Polymulti_ll.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//Polynomial multiplication using Linkedlist
/*
Algorithm
Define a structure to represent each term in the polynomial
Define a function to create a new term with a given coefficient and exponent
Define a function to insert a new term into a polynomial in order of decreasing exponent
Define a function to multiply two polynomials:
a. Create an empty linked list to represent the result polynomial
b. Iterate over each term in the first polynomial:
i. Iterate over each term in the second polynomial:
1. Multiply the coefficients of the current terms and add their exponents
2. Insert the result term into the result polynomial using the insertTerm function
c. Return the result polynomial
Define a function to display a polynomial
In main function:
a. Create two polynomials using the insertTerm function
b. Multiply the two polynomials using the multiplyPolynomials function
c. Display the result using the displayPolynomial function
*/
//Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int coefficient,exponent;
struct node *next;
};
struct node *hPtr1,*hPtr2,*hPtr3;
struct node *buildNode(int coefficient, int exponent)
{
struct node *ptr=(struct node *)malloc(sizeof(struct node));
ptr->coefficient=coefficient;
ptr->exponent=exponent;
ptr->next=NULL;
return ptr;
}
void polynomial_insert(struct node ** myNode,int coefficient,int exponent)
{
struct node *lPtr,*pPtr,*qPtr=*myNode;
lPtr=buildNode(coefficient,exponent);
if (*myNode==NULL || (*myNode)->exponent<exponent)
{
*myNode=lPtr;
(*myNode)->next=qPtr;
return;
}
while(qPtr)
{
pPtr=qPtr;
qPtr=qPtr->next;
if(!qPtr)
{
pPtr->next=lPtr;
break;
}
else if((exponent<pPtr->exponent) && (exponent>qPtr->exponent))
{
lPtr->next = qPtr;
pPtr->next = lPtr;
break;
}
}
return;
}
void polynomial_add(struct node **n1,int coefficient,int exponent)
{
struct node *x=NULL,*temp=*n1;
if (*n1==NULL || (*n1)->exponent<exponent)
{
*n1=x=buildNode(coefficient,exponent);
(*n1)->next=temp;
}
else
{
while(temp)
{
if(temp->exponent==exponent)
{
temp->coefficient=temp->coefficient+coefficient;
return;
}
if(temp->exponent>exponent && (!temp->next || temp->next->exponent<exponent))
{
x=buildNode(coefficient,exponent);
x->next=temp->next;
temp->next=x;
return;
}
temp=temp->next;
}
x->next=NULL;
temp->next=x;
}
}
void polynomial_multiply(struct node **n1,struct node *n2,struct node *n3)
{
struct node * temp;
int coefficient, exponent;
temp = n3;
if(!n2 && !n3)
{
return;
}
if(!n2)
{
*n1 = n3;
}
else if(!n3)
{
*n1 = n2;
}
else
{
while(n2)
{
while(n3)
{
coefficient = n2->coefficient * n3->coefficient;
exponent = n2->exponent + n3->exponent;
n3 = n3->next;
polynomial_add(n1, coefficient, exponent);
}
n3 = temp;
n2 = n2->next;
}
}
return;
}
struct node *polynomial_deleteList(struct node *ptr)
{
struct node *temp;
while(ptr)
{
temp=ptr->next;
free(ptr);
ptr = temp;
}
return NULL;
}
void polynomial_view(struct node *ptr)
{
int i = 0;
int flag=0;
while (ptr)
{
if(ptr->exponent!=0 || ptr->exponent!= 1)
{
if(ptr->coefficient>0 && flag==0)
{
printf("%dx^%d", ptr->coefficient,ptr->exponent);
flag++;
}
else if(ptr->coefficient>0 && flag==1)
{
printf("+%dx^%d", ptr->coefficient,ptr->exponent);
}
else if(ptr->coefficient<0)
{
printf("%dx^%d",ptr->coefficient,ptr->exponent);
}
}
else if(ptr->exponent==0)
{
if(ptr->coefficient>0 && flag==0 )
{
printf("%d",ptr->coefficient);
flag++;
}
else if(ptr->coefficient>0 && flag==1)
{
printf("+%d", ptr->coefficient);
}
else if(ptr->coefficient < 0)
{
printf("%d", ptr->coefficient);
}
}
else if(ptr->exponent==1)
{
if(ptr->coefficient>0 && flag==0)
{
printf("%dx",ptr->coefficient);
flag++;
}
else if(ptr->coefficient > 0 && flag==1)
{
printf("+%dx", ptr->coefficient);
}
else if(ptr->coefficient < 0)
{
printf("%dx", ptr->coefficient);
}
}
ptr=ptr->next;
i++;
}
printf("\n");
return;
}
int main(int argc,char *argv[])
{
int coefficient,exponent,i,n;
int count;
printf("Multiplication of Two Polynomials\n");
printf("Enter the number of coefficients in the multiplicand:");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("Enter the coefficient part:");
scanf("%d", &coefficient);
printf("Enter the exponent part:");
scanf("%d",&exponent);
polynomial_insert(&hPtr1, coefficient, exponent);
}
printf("Enter the number of coefficients in the multiplier:");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("Enter the coefficient part:");
scanf("%d", &coefficient);
printf("Enter the exponent part:");
scanf("%d",&exponent);
polynomial_insert(&hPtr2, coefficient, exponent);
}
printf("Polynomial Expression 1: ");
polynomial_view(hPtr1);
printf("Polynomial Expression 2: ");
polynomial_view(hPtr2);
polynomial_multiply(&hPtr3, hPtr1, hPtr2);
printf("Output:\n");
polynomial_view(hPtr3);
hPtr1 = polynomial_deleteList(hPtr1);
hPtr2 = polynomial_deleteList(hPtr2);
hPtr3 = polynomial_deleteList(hPtr3);
return 0;
}
/*
Solution
Multiplication of Two Polynomials
Enter the number of coefficients in the multiplicand:3
Enter the coefficient part:2
Enter the exponent part:4
Enter the coefficient part:5
Enter the exponent part:2
Enter the coefficient part:6
Enter the exponent part:1
Enter the number of coefficients in the multiplier:4
Enter the coefficient part:3
Enter the exponent part:5
Enter the coefficient part:1
Enter the exponent part:3
Enter the coefficient part:8
Enter the exponent part:2
Enter the coefficient part:3
Enter the exponent part:0
Polynomial Expression 1: 2x^4+5x^2+6x^1
Polynomial Expression 2: 3x^5+1x^3+8x^2+3x^0
Output:
6x^9+17x^7+34x^6+5x^5+52x^4+48x^3+15x^2+18x^1
*/