-
Notifications
You must be signed in to change notification settings - Fork 0
/
8. Polynomial Addition and Multiplication using Linked List
286 lines (232 loc) · 8 KB
/
8. Polynomial Addition and Multiplication using Linked List
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
277
278
279
280
281
282
283
284
285
286
/*********************************************************************************
TITLE: Program to implement polynomial addition and multiplication using LL.
*********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int coeff; //to store coefficient
int expn; //to store exponent
struct node *next; //next node
}Node;
typedef struct
{
Node *start; //LL of Polynomial
}Poly;
void create_poly(Poly *ptr)
{
Node *p, *prev;
int ch;
do
{
p=(Node*)malloc(sizeof(Node));
printf("\n Enter the coefficient : ");
scanf("%d", &p->coeff);
printf(" Enter its exponent : ");
scanf("%d", &p->expn);
p->next=NULL;
if(ptr->start==NULL)//is start NULL
{
ptr->start=p; //point it by start
}
else
{
prev->next = p;
}
prev = p; //current node in this iteration becomes previous node in next iteration
scanf("%d",&ch);
}
while(ch==1);
}
void display_poly(Poly py)
{
Node *ptr;
ptr=py.start;
while(ptr!=NULL)
{
printf(" %dx^%d ",ptr->coeff,ptr->expn); // display as 5x^3
if(ptr->next != NULL) //if first or intermediate node
printf(" + "); //separate the terms by putting '+'
ptr=ptr->next; //go to next node
} }
void append_node(Poly *ptr, int n, int c)//same as inserting node at end in LL program
{
Node *q, *p;
p=(Node*)malloc(sizeof(Node));
p->coeff=n;
p->expn=c;
p->next=NULL;
if(ptr->start==NULL)
{
ptr->start=p;
}
else
{
q = ptr->start; //start traversing from first node
while(q->next!=NULL) //until u reach to last node
q=q->next;
q->next=p; //last node gets linked with p
}
}
void add_two_poly(Poly *ans,Poly py1,Poly py2) //ans is a pointer to p3 variable
{
int sum;
Node *ptr1=py1.start,*ptr2=py2.start;
//ptr1 for traversing polynomial 1
//ptr2 for traversing polynomial 2
while(ptr1!=NULL && ptr2!=NULL) // if two nodes in both polynomial present
{
if(ptr1->expn==ptr2->expn) //if expn of terms is same
{
sum = ptr1->coeff+ptr2->coeff;//add coefficient
append_node(ans,sum,ptr1->expn); //add node in p3 polynomial
ptr1=ptr1->next;
ptr2=ptr2->next;
}
else if (ptr1->expn > ptr2->expn) //if expn greater, then append node in p3 polynomial with highest exponent
{
append_node(ans,ptr1->coeff,ptr1->expn);
ptr1=ptr1->next;
}
else //if (ptr1->expn < ptr2->expn)
{
append_node(ans,ptr2->coeff,ptr2->expn);
ptr2=ptr2->next;
}
}//end of while
if(ptr1==NULL) //if first polynomial is exhausted
{
while(ptr2!=NULL) //copy lower order terms of second polynomial to P3
{
append_node(ans,ptr2->coeff,ptr2->expn);
ptr2=ptr2->next;
}
}
if(ptr2==NULL) //if second polynomial is exhausted
{
while(ptr1!=NULL) //copy first polynomial to p3
{
append_node(ans,ptr1->coeff,ptr1->expn);
ptr1=ptr1->next;
}
}
}
void mul_two_poly(Poly *ans, Poly p1, Poly p2)
{
int coeff,expn;
Node*ptr1=p1.start;
Node*ptr2=p2.start;;
while(ptr1!=NULL)
{
while(ptr2!=NULL)
{
coeff=ptr1->coeff*ptr2->coeff;
expn=ptr1->expn+ptr2->expn;
append_node(ans,coeff,expn);
ptr2=ptr2->next;
}
ptr2=p2.start;
ptr1=ptr1->next;
}
}
int main()
{
Poly p1 , p2 , p3, p4;
p1.start=NULL;
p2.start=NULL;
p3.start=NULL;
p4.start=NULL;
int option;
printf("----------------------------------------------MENU----------------------------------------------");
printf("\n 1. Create first polynomial");
printf("\n 2. Create second polynomial");
printf("\n 3. Add the polynomials");
printf("\n 4. Multiply the polynomials");
printf("\n 5. Exit");
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the option: ");
scanf("%d", &option);
do
{
switch(option)
{
case 1:
printf("\n For polynomial 1 :\n");
create_poly(&p1);
printf("------------------------------------------------------------------------------------------------");
printf("\n First polynomial is : ");
display_poly(p1);
printf("\n------------------------------------------------------------------------------------------------\n");
break;
case 2:
printf("\n For polynomial 2 :\n");
create_poly(&p2);
printf("------------------------------------------------------------------------------------------------");
printf("\n Second polynomial is : ");
display_poly(p2);
printf("\n------------------------------------------------------------------------------------------------\n");
break;
case 3:
add_two_poly(&p3,p1, p2);
printf("------------------------------------------------------------------------------------------------");
printf("\n Resultant polynomial after addition: ");
display_poly(p3);
printf("\n------------------------------------------------------------------------------------------------\n");
break;
case 4:
mul_two_poly(&p4,p1, p2);
printf("------------------------------------------------------------------------------------------------");
printf("\n Resultant polynomial after multiplication: ");
display_poly(p4);
printf("\n------------------------------------------------------------------------------------------------\n");
break;
case 5:
exit(0);
}
printf("\n Enter the option: ");
scanf("%d", &option);
}while(1);
return 0;
}
/*********************************************************************************
OUTPUT:
----------------------------------------------MENU----------------------------------------------
1. Create first polynomial
2. Create second polynomial
3. Add the polynomials
4. Multiply the polynomials
5. Exit
------------------------------------------------------------------------------------------------
Enter the option: 1
For polynomial 1 :
Enter the coefficient : 2
Enter its exponent : 4
Enter 1 to continue creation of list and 0 to end it : 1
Enter the coefficient : 4
Enter its exponent : 2
Enter 1 to continue creation of list and 0 to end it : 0
------------------------------------------------------------------------------------------------
First polynomial is : 2x^4 + 4x^2
------------------------------------------------------------------------------------------------
Enter the option: 2
For polynomial 2 :
Enter the coefficient : 6
Enter its exponent : 3
Enter 1 to continue creation of list and 0 to end it : 1
Enter the coefficient : 2
Enter its exponent : 2
Enter 1 to continue creation of list and 0 to end it : 0
------------------------------------------------------------------------------------------------
Second polynomial is : 6x^3 + 2x^2
------------------------------------------------------------------------------------------------
Enter the option: 3
------------------------------------------------------------------------------------------------
Resultant polynomial after addition: 2x^4 + 6x^3 + 6x^2
------------------------------------------------------------------------------------------------
Enter the option: 4
------------------------------------------------------------------------------------------------
Resultant polynomial after multiplication: 12x^7 + 4x^6 + 24x^5 + 8x^4
------------------------------------------------------------------------------------------------
Enter the option: 5
Process returned 0
*********************************************************************************/