Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
poly_add.c File Reference

Implementation of [Addition of two polynomials] (https://en.wikipedia.org/wiki/Polynomial#Addition) More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for poly_add.c:

Data Structures

struct  term
 identifier for single-variable polynomial coefficients as a linked list More...
 

Functions

void free_poly (struct term *poly)
 Frees memory space.
 
void create_polynomial (struct term **poly, int coef, int pow)
 The function will create a polynomial.
 
void poly_add (struct term **pol, struct term *poly1, struct term *poly2)
 The function will add 2 polynomials.
 
void display_polynomial (struct term *poly)
 The function will display the polynomial.
 
static void test1 (struct term *poly1, struct term *poly2, struct term *poly3)
 Test function 1.
 
static void test2 (struct term *poly1, struct term *poly2, struct term *poly3)
 Test function 2.
 
static void test3 (struct term *poly1, struct term *poly2, struct term *poly3)
 Test function 3.
 
int main (void)
 Main function.
 

Detailed Description

Implementation of [Addition of two polynomials] (https://en.wikipedia.org/wiki/Polynomial#Addition)

Author
Ankita Roy Chowdhury

This code takes two polynomials as input and prints their sum using linked list. The polynomials must be in increasing or decreasing order of degree. Degree must be positive.

Function Documentation

◆ create_polynomial()

void create_polynomial ( struct term **  poly,
int  coef,
int  pow 
)

The function will create a polynomial.

Parameters
polystores the address of the polynomial being created
coefcontains the coefficient of the node
powcontains the degree
Returns
none
49{
50 // Creating the polynomial using temporary linked lists
51 struct term **temp1 = poly;
52
53 while (*temp1)
54 {
55 temp1 = &(*temp1)->next;
56 }
57
58 // Now temp1 reaches to the end of the list
59 *temp1 = (struct term *)malloc(
60 sizeof(struct term)); // Create the term and linked as the tail
61 (*temp1)->coef = coef;
62 (*temp1)->pow = pow;
63 (*temp1)->next = NULL;
64}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
identifier for single-variable polynomial coefficients as a linked list
Definition poly_add.c:20
int pow
power of the polynomial term
Definition poly_add.c:22
int coef
coefficient value
Definition poly_add.c:21
struct term * next
pointer to the successive term
Definition poly_add.c:23

◆ display_polynomial()

void display_polynomial ( struct term poly)

The function will display the polynomial.

Parameters
polyfirst term of the polynomial to be displayed
Returns
none
165{
166 while (poly != NULL)
167 {
168 printf("%d x^%d", poly->coef, poly->pow);
169 poly = poly->next;
170 if (poly != NULL)
171 {
172 printf(" + ");
173 }
174 }
175}

◆ free_poly()

void free_poly ( struct term poly)

Frees memory space.

Parameters
polyfirst term of polynomial
Returns
void
32{
33 while (poly)
34 {
35 struct term *next = poly->next;
36 free(poly);
37 poly = next;
38 }
39}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
int next(Vector *vec)
This function gets the next item from the Vector each time it's called.
Definition vector.c:102
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
294{
295 struct term *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
296 test1(poly1, poly2, poly3);
297 test2(poly1, poly2, poly3);
298 test3(poly1, poly2, poly3);
299
300 return 0;
301}
void test2()
Definition k_means_clustering.c:356
void test1()
Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM t...
Definition kohonen_som_topology.c:406
void test3()
Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM ...
Definition kohonen_som_topology.c:609
Here is the call graph for this function:

◆ poly_add()

void poly_add ( struct term **  pol,
struct term poly1,
struct term poly2 
)

The function will add 2 polynomials.

Parameters
poly1first polynomial of the addition
poly2second polynomial of the addition
polthe resultant polynomial
74{
75 // Creating a temporary linked list to store the resultant polynomial
76 struct term *temp = (struct term *)malloc(sizeof(struct term));
77 temp->next = NULL;
78 *pol =
79 temp; //*pol always points to the 1st node of the resultant polynomial
80
81 // Comparing the powers of the nodes of both the polynomials
82 // until one gets exhausted
83 while (poly1 && poly2)
84 {
85 /* If the power of the first polynomial is greater than the power of the
86 second one place the power and coefficient of that node of the first
87 polynomial in temp and increase the pointer poly1
88 */
89 if (poly1->pow > poly2->pow)
90 {
91 temp->coef = poly1->coef;
92 temp->pow = poly1->pow;
93 poly1 = poly1->next;
94 }
95 /* If the power of the second polynomial is greater than the power of
96 the first one place the power and coefficient of that node of the
97 second polynomial in temp and increase the pointer poly2
98 */
99 else if (poly1->pow < poly2->pow)
100 {
101 temp->coef = poly2->coef;
102 temp->pow = poly2->pow;
103 poly2 = poly2->next;
104 }
105 /* If both of them have same power then sum the coefficients
106 place both the summed coefficient and the power in temp
107 increase both the pointers poly1 and poly2
108 */
109 else
110 {
111 temp->coef = poly1->coef + poly2->coef;
112 temp->pow = poly1->pow;
113 poly1 = poly1->next;
114 poly2 = poly2->next;
115 }
116 /* If none of the polynomials are exhausted
117 dynamically create a node in temp
118 */
119 if (poly1 && poly2)
120 {
121 temp->next = (struct term *)malloc(
122 sizeof(struct term)); // Dynamic node creation
123 temp = temp->next; // Increase the pointer temp
124 temp->next = NULL;
125 }
126 }
127 /* If one of the polynomials is exhausted
128 place the rest of the other polynomial as it is in temp
129 by creating nodes dynamically
130 */
131 while (poly1 || poly2)
132 {
133 temp->next = (struct term *)malloc(
134 sizeof(struct term)); // Dynamic node creation
135 temp = temp->next; // Increasing the pointer
136 temp->next = NULL;
137
138 /* If poly1 is not exhausted
139 place rest of that polynomial in temp
140 */
141 if (poly1)
142 {
143 temp->coef = poly1->coef;
144 temp->pow = poly1->pow;
145 poly1 = poly1->next;
146 }
147 /* If poly2 is not exhausted
148 place rest of that polynomial in temp
149 */
150 else if (poly2)
151 {
152 temp->coef = poly2->coef;
153 temp->pow = poly2->pow;
154 poly2 = poly2->next;
155 }
156 }
157}
struct node * next
List pointers.
Definition bfs.c:24

◆ test1()

static void test1 ( struct term poly1,
struct term poly2,
struct term poly3 
)
static

Test function 1.

Polynomial 1 is 5 x^2 + 3 x^1 + 2 x^0 Polynomial 2 is 7 x^3 + 9 x^1 + 10 x^0 Resultant polynomial is 7 x^3 + 5 x^2 + 12 x^1 + 12 x^0

Returns
void
187{
188 printf("\n----Test 1----\n");
189 printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
190 create_polynomial(&poly1, 5, 2);
191 create_polynomial(&poly1, 3, 1);
192 create_polynomial(&poly1, 2, 0);
193 display_polynomial(poly1);
194
195 printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
196 create_polynomial(&poly2, 7, 3);
197 create_polynomial(&poly2, 9, 1);
198 create_polynomial(&poly2, 10, 0);
199 display_polynomial(poly2);
200
201 poly_add(&poly3, poly1, poly2); // Adding the two polynomials
202 printf("\nResultant polynomial:\n");
203 display_polynomial(poly3);
204 printf("\n");
205
206 // Frees memory space
207 free_poly(poly1);
208 free_poly(poly2);
209 free_poly(poly3);
210}
void create_polynomial(struct term **poly, int coef, int pow)
The function will create a polynomial.
Definition poly_add.c:48
void free_poly(struct term *poly)
Frees memory space.
Definition poly_add.c:31
void poly_add(struct term **pol, struct term *poly1, struct term *poly2)
The function will add 2 polynomials.
Definition poly_add.c:73
void display_polynomial(struct term *poly)
The function will display the polynomial.
Definition poly_add.c:164
Here is the call graph for this function:

◆ test2()

static void test2 ( struct term poly1,
struct term poly2,
struct term poly3 
)
static

Test function 2.

Polynomial 1 is 3 x^5 + 1 x^4 + 2 x^3 + -2 x^1 + 5 x^0 Polynomial 2 is 2 x^5 + 3 x^3 + 7 x^1 + 2 x^0 Resultant polynomial is 5 x^5 + 1 x^4 + 5 x^3 + 5 x^1 + 7 x^0

Returns
void
222{
223 printf("\n----Test 2----\n");
224 printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
225 create_polynomial(&poly1, 3, 5);
226 create_polynomial(&poly1, 1, 4);
227 create_polynomial(&poly1, 2, 3);
228 create_polynomial(&poly1, -2, 1);
229 create_polynomial(&poly1, 5, 0);
230
231 display_polynomial(poly1);
232
233 printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
234 create_polynomial(&poly2, 2, 5);
235 create_polynomial(&poly2, 3, 3);
236 create_polynomial(&poly2, 7, 1);
237 create_polynomial(&poly2, 2, 0);
238
239 display_polynomial(poly2);
240
241 poly_add(&poly3, poly1, poly2); // Adding the two polynomials
242 printf("\nResultant polynomial:\n");
243 display_polynomial(poly3);
244 printf("\n");
245
246 // Frees memory space
247 free_poly(poly1);
248 free_poly(poly2);
249 free_poly(poly3);
250}
Here is the call graph for this function:

◆ test3()

static void test3 ( struct term poly1,
struct term poly2,
struct term poly3 
)
static

Test function 3.

Polynomial 1 is -12 x^0 + 8 x^1 + 4 x^3 Polynomial 2 is 5 x^0 + -13 x^1 + 3 x^3 Resultant polynomial is -7 x^0 + -5 x^1 + 7 x^3

Returns
void
262{
263 printf("\n----Test 3----\n");
264 printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
265 create_polynomial(&poly1, -12, 0);
266 create_polynomial(&poly1, 8, 1);
267 create_polynomial(&poly1, 4, 3);
268
269 display_polynomial(poly1);
270
271 printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
272 create_polynomial(&poly2, 5, 0);
273 create_polynomial(&poly2, -13, 1);
274 create_polynomial(&poly2, 3, 3);
275
276 display_polynomial(poly2);
277
278 poly_add(&poly3, poly1, poly2); // Adding the two polynomials
279 printf("\nResultant polynomial:\n");
280 display_polynomial(poly3);
281 printf("\n");
282
283 // Frees memory space
284 free_poly(poly1);
285 free_poly(poly2);
286 free_poly(poly3);
287}
Here is the call graph for this function: