forked from guptautkarsh64/Hacktober-Fest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_ll.c
236 lines (182 loc) · 4.5 KB
/
double_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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int power; // power of polynomial
int cf; // coeffecient
struct Node* next;
};
void display(struct Node* ptr)
{
printf("%dx^%d", ptr->cf, ptr->power);
ptr = ptr->next;
while (ptr != NULL)
{
printf(" + %dx^%d", ptr->cf, ptr->power);
ptr = ptr->next;
}
printf("\n");
}
struct Node* init(int power, int cf)
{
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->power = power;
p->cf = cf;
p->next = NULL;
return p;
}
struct Node* insertEnd(struct Node* head, int power, int cf)
{
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
struct Node* p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = ptr;
ptr->power = power;
ptr->cf = cf;
ptr->next = NULL;
return head;
}
struct Node* deleteFirst(struct Node* head)
{
struct Node* p;
p = head->next;
free(head);
return p;
}
struct Node* get_poly(struct Node* p, int a, int deg)
{
for (int i = 0; i < deg + 1; i++)
{
int cf;
if (i > a)
{
p = insertEnd(p, i, 0);
}
else
{
printf("Enter coefficient of %d\n", i);
scanf("%d", &cf);
if (i == 0)
{
p = init(i, cf);
}
else
{
p = insertEnd(p, i, cf);
}
}
}
return p;
}
struct Node* add(struct Node* p1, struct Node* p2)
{
struct Node* sum;
int cf = p1->cf + p2->cf;
sum = init(p1->power, cf);
p1 = p1->next;
p2 = p2->next;
while (p1 != NULL)
{
// sum->cf = p1->cf + p2->cf;
// sum->power = p1->power;
cf = p1->cf + p2->cf;
sum = insertEnd(sum, p1->power, cf);
p1 = p1->next;
p2 = p2->next;
}
return sum;
}
struct Node* sort(struct Node* p, int deg)
{
struct Node* res;
res = (struct Node*)malloc(sizeof(struct Node));
res = init(0, 0);
for (int i = 0; i < deg + 1; i++)
{
struct Node* temp = p;
int cf = 0;
while (temp != NULL)
{
if (temp->power == i)
{
cf += temp->cf;
}
temp = temp->next;
}
res = insertEnd(res, i, cf);
}
res = deleteFirst(res);
return res;
}
struct Node* multiply(struct Node* p1, struct Node* p2, int deg)
{
struct Node* product = (struct Node*)malloc(sizeof(struct Node));
struct Node* result = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = p2;
int cf;
int power;
while (p1 != NULL)
{
temp = p2;
while (temp != NULL)
{
cf = p1->cf * temp->cf; // product of two coeffecients
power = p1->power + temp->power; // product of powers
// printf("Power%d :%d x %d = %d\n",power,p1->cf,temp->cf,cf);
product = insertEnd(product, power, cf);
temp = temp->next;
}
p1 = p1->next;
}
deleteFirst(product); // Delete the first empty node
result = sort(product, deg);
return result;
}
int max(int a, int b)
{
return (a >= b) ? a : b;
}
int main()
{
struct Node* p1;
struct Node* p2;
struct Node* sum;
struct Node* result;
int a, b, deg;
// Enter degree of polynomial p1
printf("Enter degree of polynomial p1\n");
scanf("%d", &a);
// Enter degree of polynomial p2
printf("Enter degree of polynomial p2\n");
scanf("%d", &b);
deg = max(a, b);
// Enter coefficients of p1
printf("Enter coefficients of p1\n");
p1 = get_poly(p1, a, deg);
display(p1);
// Enter coefficients of p2
printf("Enter coefficients of p2\n");
p2 = get_poly(p2, b, deg);
display(p2);
int choice;
printf("\nPress:\n1 for Addition\n2 for Multiplication\n");
scanf("%d", &choice);
if (choice == 1)
{
// add two polynomials
printf("Sum of p1 and p2\n");
sum = add(p1, p2);
display(sum);
}
else if (choice == 2)
{
// multiply two polynomials
printf("Product of p1 and p2\n");
result = multiply(p1, p2, a + b);
display(result);
}
return 0;
}