Skip to content

Commit 49e8f4a

Browse files
Ankita19ms0010github-actionsPanquesito7
authored
feat:Add Polynomial Addition (#600)
* feat:Add Polynomial Addition * Review changes * updating DIRECTORY.md * Apply suggestions from code review Co-authored-by: David Leal <[email protected]> * Corrected printing Co-authored-by: David Leal <[email protected]> * file path fixed * updating DIRECTORY.md * Corrected free memory Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <[email protected]>
1 parent 3052511 commit 49e8f4a

File tree

2 files changed

+320
-0
lines changed

2 files changed

+320
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
* [Mirror](https://github.com/TheAlgorithms/C/blob/master/misc/mirror.c)
249249
* [Palindrome](https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c)
250250
* [Pid](https://github.com/TheAlgorithms/C/blob/master/misc/pid.c)
251+
* [Poly Add](https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c)
251252
* [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c)
252253
* [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c)
253254
* [Quartile](https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c)

misc/poly_add.c

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
/**
2+
* @file
3+
* @brief Implementation of [Addition of two polynomials]
4+
* (https://en.wikipedia.org/wiki/Polynomial#Addition)
5+
* @author [Ankita Roy Chowdhury](https://github.com/Ankita19ms0010)
6+
* @details
7+
* This code takes two polynomials as input
8+
* and prints their sum using linked list.
9+
* The polynomials must be in increasing or decreasing order of degree.
10+
* Degree must be positive.
11+
*/
12+
#include <stdio.h> // for io operations
13+
#include <stdlib.h>
14+
15+
/**
16+
* @brief identifier for single-variable polynomial coefficients as a linked
17+
* list
18+
*/
19+
struct term
20+
{
21+
int coef; /**< coefficient value */
22+
int pow; /**< power of the polynomial term */
23+
struct term *next; /**< pointer to the successive term */
24+
};
25+
26+
/**
27+
* @brief Frees memory space
28+
* @param poly first term of polynomial
29+
* @returns void
30+
*/
31+
void free_poly(struct term *poly)
32+
{
33+
if (!poly)
34+
{
35+
return; // NULL pointer does not need delete
36+
}
37+
else
38+
{
39+
while (!poly->next)
40+
{
41+
free(poly->next); // Deletes next term
42+
}
43+
free(poly); // delete the current term
44+
}
45+
}
46+
47+
/**
48+
* The function will create a polynomial
49+
* @param poly stores the address of the polynomial being created
50+
* @param coef contains the coefficient of the node
51+
* @param pow contains the degree
52+
* @returns none
53+
*/
54+
void create_polynomial(struct term **poly, int coef, int pow)
55+
{
56+
// Creating the polynomial using temporary linked lists
57+
struct term *temp1, *temp2;
58+
temp1 = *poly; // Contains the null pointer
59+
60+
// Initiating first term
61+
if (temp1 == NULL)
62+
{
63+
temp2 = (struct term *)malloc(
64+
sizeof(struct term)); // Dynamic node creation
65+
temp2->coef = coef;
66+
temp2->pow = pow;
67+
// Updating the null pointer with the address of the first node of the
68+
// polynomial just created
69+
*poly = temp2;
70+
temp2->next = NULL; // Increasing the pointer temp2
71+
}
72+
// Creating the rest of the nodes
73+
else
74+
{
75+
temp2->next = (struct term *)malloc(
76+
sizeof(struct term)); // Dynamic node creation
77+
temp2 = temp2->next; // Increasing the pointer temp2
78+
temp2->coef = coef;
79+
temp2->pow = pow;
80+
temp2->next = NULL;
81+
}
82+
}
83+
84+
/**
85+
* The function will add 2 polynomials
86+
* @param poly1 first polynomial of the addition
87+
* @param poly2 second polynomial of the addition
88+
* @param pol the resultant polynomial
89+
*/
90+
91+
void poly_add(struct term **pol, struct term *poly1, struct term *poly2)
92+
{
93+
// Creating a temporary linked list to store the resultant polynomial
94+
struct term *temp = (struct term *)malloc(sizeof(struct term));
95+
temp->next = NULL;
96+
*pol =
97+
temp; //*pol always points to the 1st node of the resultant polynomial
98+
99+
// Comparing the powers of the nodes of both the polynomials
100+
// until one gets exhausted
101+
while (poly1 && poly2)
102+
{
103+
/* If the power of the first polynomial is greater than the power of the
104+
second one place the power and coefficient of that node of the first
105+
polynomial in temp and increase the pointer poly1
106+
*/
107+
if (poly1->pow > poly2->pow)
108+
{
109+
temp->coef = poly1->coef;
110+
temp->pow = poly1->pow;
111+
poly1 = poly1->next;
112+
}
113+
/* If the power of the second polynomial is greater than the power of
114+
the first one place the power and coefficient of that node of the
115+
second polynomial in temp and increase the pointer poly2
116+
*/
117+
else if (poly1->pow < poly2->pow)
118+
{
119+
temp->coef = poly2->coef;
120+
temp->pow = poly2->pow;
121+
poly2 = poly2->next;
122+
}
123+
/* If both of them have same power then sum the coefficients
124+
place both the summed coefficient and the power in temp
125+
increase both the pointers poly1 and poly2
126+
*/
127+
else
128+
{
129+
temp->coef = poly1->coef + poly2->coef;
130+
temp->pow = poly1->pow;
131+
poly1 = poly1->next;
132+
poly2 = poly2->next;
133+
}
134+
/* If none of the polynomials are exhausted
135+
dynamically create a node in temp
136+
*/
137+
if (poly1 && poly2)
138+
{
139+
temp->next = (struct term *)malloc(
140+
sizeof(struct term)); // Dynamic node creation
141+
temp = temp->next; // Increase the pointer temp
142+
temp->next = NULL;
143+
}
144+
}
145+
/* If one of the polynomials is exhausted
146+
place the rest of the other polynomial as it is in temp
147+
by creating nodes dynamically
148+
*/
149+
while (poly1 || poly2)
150+
{
151+
temp->next = (struct term *)malloc(
152+
sizeof(struct term)); // Dynamic node creation
153+
temp = temp->next; // Increasing the pointer
154+
temp->next = NULL;
155+
156+
/* If poly1 is not exhausted
157+
place rest of that polynomial in temp
158+
*/
159+
if (poly1)
160+
{
161+
temp->coef = poly1->coef;
162+
temp->pow = poly1->pow;
163+
poly1 = poly1->next;
164+
}
165+
/* If poly2 is not exhausted
166+
place rest of that polynomial in temp
167+
*/
168+
else if (poly2)
169+
{
170+
temp->coef = poly2->coef;
171+
temp->pow = poly2->pow;
172+
poly2 = poly2->next;
173+
}
174+
}
175+
}
176+
177+
/**
178+
* The function will display the polynomial
179+
* @param poly first term of the polynomial to be displayed
180+
* @returns none
181+
*/
182+
void display_polynomial(struct term *poly)
183+
{
184+
while (poly != NULL)
185+
{
186+
printf("%d x^%d", poly->coef, poly->pow);
187+
poly = poly->next;
188+
if (poly != NULL)
189+
{
190+
printf(" + ");
191+
}
192+
}
193+
}
194+
195+
/**
196+
* @brief Test function 1
197+
*
198+
* @details
199+
* Polynomial 1 is 5 x^2 + 3 x^1 + 2 x^0
200+
* Polynomial 2 is 7 x^3 + 9 x^1 + 10 x^0
201+
* Resultant polynomial is 7 x^3 + 5 x^2 + 12 x^1 + 12 x^0
202+
* @returns void
203+
*/
204+
static void test1(struct term *poly1, struct term *poly2, struct term *poly3)
205+
{
206+
printf("\n----Test 1----\n");
207+
printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
208+
create_polynomial(&poly1, 5, 2);
209+
create_polynomial(&poly1, 3, 1);
210+
create_polynomial(&poly1, 2, 0);
211+
display_polynomial(poly1);
212+
213+
printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
214+
create_polynomial(&poly2, 7, 3);
215+
create_polynomial(&poly2, 9, 1);
216+
create_polynomial(&poly2, 10, 0);
217+
display_polynomial(poly2);
218+
219+
poly_add(&poly3, poly1, poly2); // Adding the two polynomials
220+
printf("\nResultant polynomial:\n");
221+
display_polynomial(poly3);
222+
printf("\n");
223+
224+
// Frees memory space
225+
free_poly(poly1);
226+
free_poly(poly2);
227+
free_poly(poly3);
228+
}
229+
230+
/**
231+
* @brief Test function 2
232+
*
233+
* @details
234+
* Polynomial 1 is 3 x^5 + 1 x^4 + 2 x^3 + -2 x^1 + 5 x^0
235+
* Polynomial 2 is 2 x^5 + 3 x^3 + 7 x^1 + 2 x^0
236+
* Resultant polynomial is 5 x^5 + 1 x^4 + 5 x^3 + 5 x^1 + 7 x^0
237+
* @returns void
238+
*/
239+
static void test2(struct term *poly1, struct term *poly2, struct term *poly3)
240+
{
241+
printf("\n----Test 2----\n");
242+
printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
243+
create_polynomial(&poly1, 3, 5);
244+
create_polynomial(&poly1, 1, 4);
245+
create_polynomial(&poly1, 2, 3);
246+
create_polynomial(&poly1, -2, 1);
247+
create_polynomial(&poly1, 5, 0);
248+
249+
display_polynomial(poly1);
250+
251+
printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
252+
create_polynomial(&poly2, 2, 5);
253+
create_polynomial(&poly2, 3, 3);
254+
create_polynomial(&poly2, 7, 1);
255+
create_polynomial(&poly2, 2, 0);
256+
257+
display_polynomial(poly2);
258+
259+
poly_add(&poly3, poly1, poly2); // Adding the two polynomials
260+
printf("\nResultant polynomial:\n");
261+
display_polynomial(poly3);
262+
printf("\n");
263+
264+
// Frees memory space
265+
free_poly(poly1);
266+
free_poly(poly2);
267+
free_poly(poly3);
268+
}
269+
270+
/**
271+
* @brief Test function 3
272+
*
273+
* @details
274+
* Polynomial 1 is -12 x^0 + 8 x^1 + 4 x^3
275+
* Polynomial 2 is 5 x^0 + -13 x^1 + 3 x^3
276+
* Resultant polynomial is -7 x^0 + -5 x^1 + 7 x^3
277+
* @returns void
278+
*/
279+
static void test3(struct term *poly1, struct term *poly2, struct term *poly3)
280+
{
281+
printf("\n----Test 3----\n");
282+
printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
283+
create_polynomial(&poly1, -12, 0);
284+
create_polynomial(&poly1, 8, 1);
285+
create_polynomial(&poly1, 4, 3);
286+
287+
display_polynomial(poly1);
288+
289+
printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
290+
create_polynomial(&poly2, 5, 0);
291+
create_polynomial(&poly2, -13, 1);
292+
create_polynomial(&poly2, 3, 3);
293+
294+
display_polynomial(poly2);
295+
296+
poly_add(&poly3, poly1, poly2); // Adding the two polynomials
297+
printf("\nResultant polynomial:\n");
298+
display_polynomial(poly3);
299+
printf("\n");
300+
301+
// Frees memory space
302+
free_poly(poly1);
303+
free_poly(poly2);
304+
free_poly(poly3);
305+
}
306+
307+
/**
308+
* @brief Main function
309+
* @returns 0 on exit
310+
*/
311+
int main(void)
312+
{
313+
struct term *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
314+
test1(poly1, poly2, poly3);
315+
test2(poly1, poly2, poly3);
316+
test3(poly1, poly2, poly3);
317+
318+
return 0;
319+
}

0 commit comments

Comments
 (0)