0% found this document useful (0 votes)
29 views10 pages

Unit2 DS Polynomial

Polynomials can be represented using linked lists and operations like addition and multiplication can be performed on them. Polynomial addition involves traversing both lists and either adding or inserting nodes based on the exponents. Polynomial multiplication iterates through each term in one polynomial and multiplies it by each term in the other, inserting the resulting terms into the output polynomial linked list. Code provided shows functions for creating nodes, inserting into a list, printing a list, and adding two polynomials represented as linked lists.

Uploaded by

namansehgal3006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

Unit2 DS Polynomial

Polynomials can be represented using linked lists and operations like addition and multiplication can be performed on them. Polynomial addition involves traversing both lists and either adding or inserting nodes based on the exponents. Polynomial multiplication iterates through each term in one polynomial and multiplies it by each term in the other, inserting the resulting terms into the output polynomial linked list. Code provided shows functions for creating nodes, inserting into a list, printing a list, and adding two polynomials represented as linked lists.

Uploaded by

namansehgal3006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Polynomials and Polynomial

Arithmetic

Data Structures
Dr. Nivedita Palia
Polynomials

Operations : Addition , Subtraction


Addition of two polynomials

The algorithm for performing the Polynomial Addition using Linked List
•Create a new linked list.
•Traverse both lists until one of them is null.
•If any list is null insert the remaining node of another list in the resultant list.
•Otherwise compare the degree of both nodes, a (first list’s node) and b (second list’s
node). Here three cases are possible:
• If the degree of a and b is equal, we insert a new node in the resultant list
with the coefficient equal to the sum of coefficients of a and b and the same
degree.
• If the degree of a is greater than b, we insert a new node in the resultant list
with the coefficient and degree equal to that of a.
Code for Polynomial Addition
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coef;
int exp;
struct Node* next; };
typedef struct Node Node;
// Creation of Polynomial
void insert(Node** poly, int coef, int exp) {
Node* temp = (Node*) malloc(sizeof(Node));
temp->coef = coef;
temp->exp = exp;
temp->next = NULL;
if (*poly == NULL) {
*poly = temp;
return; }
Node* current = *poly;
while (current->next != NULL) {
current = current->next; }
current->next = temp;}
// Printing of polynomial
void print(Node* poly) {
if (poly == NULL) {
printf("0\n");
return;
}
Node* current = poly;
while (current != NULL)
{
printf("%dx^%d", current->coef, current->exp);
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
}
printf("\n");
}
//Addition of two polynomial
Node* add(Node* poly1, Node* poly2) {
Node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->exp == poly2->exp) {
insert(&result, poly1->coef + poly2->coef, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->exp > poly2->exp) {
insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
} else {
insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next; }
}
while (poly1 != NULL) {
insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
return result;
}
int main() {
Node* poly1 = NULL;
insert(&poly1, 5, 4);
insert(&poly1, 3, 2);
insert(&poly1, 1, 0);

Node* poly2 = NULL;


insert(&poly2, 4, 4);
insert(&poly2, 2, 2);
insert(&poly2, 1, 1);

printf("First polynomial: ");


print(poly1);

printf("Second polynomial: ");


print(poly2);

Node* result = add(poly1, poly2);


printf("Result: ");
print(result);

return 0;
}
Algorithm for Polynomial
Multiplication

1.First define the node structure to hold the coefficient and exponent
values, as well as a pointer to the next node.
2.Create a function to create a new node with the specified coefficient and
exponent values.
3.Create a function to insert a new node into a linked list based on its
exponent value.
4.Create a function to print the linked list.
5.Create a function to multiply two polynomials represented as linked lists.
6.Iterate over each term in the first polynomial, multiply it with each term
in the second polynomial, and add the resulting term to the result
polynomial using the insert_node() function.
7. Return the resulting polynomial as a linked list.
END

You might also like