Polynomial Addition using Linked List: Write a C program to add 2 polynomials.
Use functions:
polyappend —- to append a term to the polynomial
polyadd — to add 2 polynomials
display — to display a polynomial.
Refer to the function specifications for more details.
The terms in the polynomial are always input in the descending order of their exponents.
Use the structure specified here to describe a term in the polynomial.
struct term { int coeff ; int expo; struct term * next; } ;
Function Specification:
void polyappend ( struct term **poly, int coeff, int expo );
struct term * polyadd(struct term *poly1, struct term *poly2);
void display(struct term *poly);
Input Output Format:
The first input corresponds to an integer m which corresponds to the number of terms in the first polynomial. The next 2 integers correspond to the coefficient and exponent of the first term in the first polynomial, the next 2 integers correspond to the coefficient and exponent of the second term in the first polynomial ….. up to m terms.
The next input corresponds to an integer n which corresponds to the number of terms in the second polynomial. The next 2 integers correspond to the coefficient and exponent of the first term in the second polynomial, the next 2 integers correspond to the coefficient and exponent of the second term in the second polynomial ….. up to nterms.
Sample Input and Output:
[All the text in bold corresponds to input and the rest corresponds to output]
Enter the number of terms in the first polynomial
5
Enter the coefficient and exponent of term 1 in the first polynomial:
1
7
Enter the coefficient and exponent of term 2 in the first polynomial:
2
6
Enter the coefficient and exponent of term 3 in the first polynomial:
3
5
Enter the coefficient and exponent of term 4 in the first polynomial:
4
4
Enter the coefficient and exponent of term 5 in the first polynomial:
5
2
Enter the number of terms in the second polynomial
5
Enter the coefficient and exponent of term 1 in the second polynomial:
1
4
Enter the coefficient and exponent of term 2 in the second polynomial:
1
3
Enter the coefficient and exponent of term 3 in the second polynomial:
1
2
Enter the coefficient and exponent of term 4 in the second polynomial:
1
1
Enter the coefficient and exponent of term 5 in the second polynomial:
2
O
First Polynomial:
1 x^7 + 2 x^6 + 3 x^5 + 4 x^4 + 5 x^2
Second Polynomial:
1 x^4 + 1 x^3 + 1 x^2 + 1 x^1 + 2
Resultant Polynomial:
1 x^7 + 2 x^6 + 3 x^5 + 5 x^4 + 1 x^3 + 6 x^2 + 1 x^1 + 2
Solution
#include<stdio.h>
#include<stdlib.h>
struct term
{
int coeff ;
int expo;
struct term * next;
} ;
void polyappend ( struct term **poly, int coeff, int expo ){
struct term * x= (struct term *) malloc(sizeof (struct term));
struct term *temp=*poly;
x->coeff=coeff;
x->expo=expo;
x->next= NULL;
if(*poly== NULL){
*poly=x;
}
else{
while(temp->next!=NULL) {
temp=temp->next;
}
temp->next=x;
}
}
struct term * polyadd(struct term *poly1, struct term *poly2){
int i;
int max_ = (poly1->expo > poly2->expo) ? poly1->expo : poly2->expo ;
int res[max_+1];
for(i = 0; i <= max_; i++) res [i] = 0;
i = 0;
while(poly1){
res[poly1->expo] += poly1->coeff;
poly1 = poly1->next;
}
while(poly2){
res[poly2->expo] += poly2->coeff;
poly2 = poly2->next;
}
struct term *nn, *new = NULL;
while(i <= max_){
nn = (struct term*)malloc(sizeof(struct term));
nn->coeff = res[i];
nn->expo = i;
if(nn->coeff){
nn->next = new;
new = nn;
}
i++;
}
return new;
}
void display(struct term *poly){
while(poly->next!=NULL){
printf("%d ",poly->coeff);
if(poly->expo!=0){
printf("x^%d ",poly->expo);
}
if(poly->next->coeff>=0){
printf("+ ");
}
poly = poly->next;
}
if(poly!=NULL){
if(poly->coeff>=0){
printf("%d",poly->coeff);
if(poly->expo!=0){
printf(" x^%d",poly->expo);
}
}
}
printf("\n");
}
int main(){
int m,n,i,coeff,expo;
struct term * poly1 = NULL;
struct term * poly2= NULL;
struct term * polySum;
printf("Enter the number of terms in the first polynomial\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the coefficient and exponent of term %d in the first polynomial:\n",i+1);
scanf("%d%d",&coeff,&expo);
polyappend(&poly1,coeff,expo);
}
printf("Enter the number of terms in the second polynomial\n");
scanf("%d",&m);
for(i=0;i<m;i++){
printf("Enter the coefficient and exponent of term %d in the second polynomial:\n",i+1);
scanf("%d%d",&coeff,&expo);
polyappend(&poly2,coeff,expo);
}
printf("First Polynomial:\n");
display(poly1);
printf("Second Polynomial:\n");
display(poly2);
polySum = polyadd(poly1,poly2);
printf("Resultant Polynomial:\n");
display(polySum);
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.