Last active
September 25, 2023 01:59
-
-
Save jamesgeorge007/336622f348815f0bef589bae5b622cff to your computer and use it in GitHub Desktop.
Polynomial Addition using Linked list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
struct Node | |
{ | |
int coeff; | |
int pow; | |
struct Node* next; | |
}; | |
void readPolynomial(struct Node** poly) | |
{ | |
int coeff, exp, cont; | |
struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); | |
*poly = temp; | |
do{ | |
printf("\n Coeffecient: "); | |
scanf("%d", &coeff); | |
printf("\n Exponent: "); | |
scanf("%d", &exp); | |
temp->coeff = coeff; | |
temp->pow = exp; | |
temp-> next = NULL; | |
printf("\nHave more terms? 1 for y and 0 for no: "); | |
scanf("%d", &cont); | |
if(cont) | |
{ | |
temp->next = (struct Node*)malloc(sizeof(struct Node)); | |
temp = temp->next; | |
temp->next = NULL; | |
} | |
}while(cont); | |
} | |
void displayPolynomial(struct Node* poly) | |
{ | |
printf("\nPolynomial expression is: "); | |
while(poly != NULL) | |
{ | |
printf("%dX^%d", poly->coeff, poly->pow); | |
poly = poly->next; | |
if(poly != NULL) | |
printf("+"); | |
} | |
} | |
void addPolynomials(struct Node** result, struct Node* first, struct Node* second) | |
{ | |
struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); | |
temp->next = NULL; | |
*result = temp; | |
while(first && second) | |
{ | |
if(first->pow > second->pow) | |
{ | |
temp->coeff = first->coeff; | |
temp->pow = first->pow; | |
first = first->next; | |
} | |
else if(first->pow < second->pow) | |
{ | |
temp->coeff = second->coeff; | |
temp->pow = second->pow; | |
second = second->next; | |
} | |
else | |
{ | |
temp->coeff = first->coeff + second->coeff; | |
temp->pow = first->pow; | |
first = first->next; | |
second = second->next; | |
} | |
if(first && second) | |
{ | |
temp->next = (struct Node*)malloc(sizeof(struct Node)); | |
temp = temp->next; | |
temp->next = NULL; | |
} | |
} | |
while(first || second) | |
{ | |
temp->next = (struct Node*)malloc(sizeof(struct Node)); | |
temp = temp->next; | |
temp->next = NULL; | |
if(first) | |
{ | |
temp->coeff = first->coeff; | |
temp->pow = first->pow; | |
first = first->next; | |
} | |
else if(second) | |
{ | |
temp->coeff = second->coeff; | |
temp->pow = second->pow; | |
second = second->next; | |
} | |
} | |
} | |
int main() | |
{ | |
struct Node* first = NULL; | |
struct Node* second = NULL; | |
struct Node* result = NULL; | |
printf("\nEnter the corresponding data:-\n"); | |
printf("\nFirst polynomial:\n"); | |
readPolynomial(&first); | |
displayPolynomial(first); | |
printf("\nSecond polynomial:\n"); | |
readPolynomial(&second); | |
displayPolynomial(second); | |
addPolynomials(&result, first, second); | |
displayPolynomial(result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
multiply two please