Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
poly_add.c File Reference

Implementation of Addition of two polynomials 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. More...
 
void create_polynomial (struct term **poly, int coef, int pow)
 The function will create a polynomial. More...
 
void poly_add (struct term **pol, struct term *poly1, struct term *poly2)
 The function will add 2 polynomials. More...
 
void display_polynomial (struct term *poly)
 The function will display the polynomial. More...
 
static void test1 (struct term *poly1, struct term *poly2, struct term *poly3)
 Test function 1. More...
 
static void test2 (struct term *poly1, struct term *poly2, struct term *poly3)
 Test function 2. More...
 
static void test3 (struct term *poly1, struct term *poly2, struct term *poly3)
 Test function 3. More...
 
int main (void)
 Main function. More...
 

Detailed Description

Implementation of Addition of two polynomials

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
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 }
#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
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 }

◆ free_poly()

void free_poly ( struct term poly)

Frees memory space.

Parameters
polyfirst term of polynomial
Returns
void
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 }
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
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 }
static void test2(struct term *poly1, struct term *poly2, struct term *poly3)
Test function 2.
Definition: poly_add.c:239
static void test3(struct term *poly1, struct term *poly2, struct term *poly3)
Test function 3.
Definition: poly_add.c:279
static void test1(struct term *poly1, struct term *poly2, struct term *poly3)
Test function 1.
Definition: poly_add.c:204
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
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 }

◆ 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
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 }
void create_polynomial(struct term **poly, int coef, int pow)
The function will create a polynomial.
Definition: poly_add.c:54
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:91
void display_polynomial(struct term *poly)
The function will display the polynomial.
Definition: poly_add.c:182
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
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 }
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
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 }
Here is the call graph for this function: