0% found this document useful (0 votes)
23 views3 pages

Ds 11

Uploaded by

anavadyapradeep
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)
23 views3 pages

Ds 11

Uploaded by

anavadyapradeep
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/ 3

Program

/* Experiment No.11 Polynomial using Linked List


Write a C program to read two polynomials and store them using a
linked list. Do the following operations on them.
a. Calculate the sum of the two polynomials and store it using a
linked list.
b. Find the product of two polynomials and store it using a linked list.
17.Anavadya Pradeep.23/09/2024 */

#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode {


int coe;
int exp;
struct PolyNode* next;
} PolyNode;

// Function to create a node


PolyNode* createNode(int coe, int exp) {
PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));
newNode->coe = coe;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}

// Function to insert a term in the polynomial


void insertTerm(PolyNode** poly, int coe, int exp) {
PolyNode* newNode = createNode(coe, exp);
if (*poly == NULL) {
*poly = newNode;
return;
}

PolyNode* current = *poly;


while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}

// Function to print the polynomial


void printPoly(PolyNode* poly) {
while (poly != NULL) {
printf("%dx^%d", poly->coe, poly->exp);
poly = poly->next;
if (poly != NULL) {
printf(" + ");
}
}
printf("\n");
}

// Function to add polynomials


PolyNode* addPoly(PolyNode* poly1, PolyNode* poly2) {
PolyNode* sumPoly = NULL;
PolyNode* current1 = poly1;
PolyNode* current2 = poly2;

while (current1 != NULL && current2 != NULL) {


if (current1->exp == current2->exp) {
insertTerm(&sumPoly, current1->coe + current2->coe, current1->exp);
current1 = current1->next;
current2 = current2->next;
} else if (current1->exp > current2->exp) {
insertTerm(&sumPoly, current1->coe, current1->exp);
current1 = current1->next;
} else {
insertTerm(&sumPoly, current2->coe, current2->exp);
current2 = current2->next;
}
}

while (current1 != NULL) {


insertTerm(&sumPoly, current1->coe, current1->exp);
current1 = current1->next;
}

while (current2 != NULL) {


insertTerm(&sumPoly, current2->coe, current2->exp);
current2 = current2->next;
}

return sumPoly;
}

// Function to multiply polynomials


PolyNode* multiplypoly(PolyNode* poly1, PolyNode* poly2) {
PolyNode* product = NULL;
PolyNode* temp1 = poly1;

while (temp1 != NULL) {


PolyNode* temp2 = poly2;
while (temp2 != NULL) {
int coe = temp1->coe * temp2->coe;
int exp = temp1->exp + temp2->exp;
insertTerm(&product, coe, exp);
temp2 = temp2->next;
}
temp1 = temp1->next;
}

PolyNode* combined = NULL;


PolyNode* temp = product;

while (temp != NULL) {


PolyNode* nextTemp = temp->next;
PolyNode* found = combined;
PolyNode* prev = NULL;

while (found != NULL) {


if (found->exp == temp->exp) {
found->coe += temp->coe;
break;
}
prev = found;
found = found->next;
}

if (found == NULL) {
insertTerm(&combined, temp->coe, temp->exp);
}
temp = nextTemp;
}

// Free the product list as it is no longer needed


temp = product;
while (temp != NULL) {
PolyNode* toFree = temp;
temp = temp->next;
free(toFree);
}

return combined;
}

int main() {
PolyNode* poly1 = NULL;
PolyNode* poly2 = NULL;
int m, n, c, e, c2, e2;

printf("Enter the number of terms in polynomial 1: ");


scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the coefficient and exponent pair of polynomial 1: ");
scanf("%d%d", &c, &e);
insertTerm(&poly1, c, e);
}

printf("Enter the number of terms in polynomial 2: ");


scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter the coefficient and exponent pair of polynomial 2: ");
scanf("%d%d", &c2, &e2);
insertTerm(&poly2, c2, e2);
}

printf("Polynomial 1: ");
printPoly(poly1);
printf("Polynomial 2: ");
printPoly(poly2);
printf("Sum: ");
PolyNode* sumPoly = addPoly(poly1, poly2);
printPoly(sumPoly);
printf("Product: ");
PolyNode*productpoly=multiplypoly(poly1,poly2);
printPoly(productpoly);
return 0;
}

Sample Output

Result
The program was executed and the output verified.

You might also like