Implement polynomial addition and multiplication using linked lists.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
float c;
int power;
struct Node *nextNode;
};
struct Node *create(struct Node *);
struct Node *insert_s(struct Node *,float,int);
struct Node *insert(struct Node *,float,int);
void printingList(struct Node *ptr);
void AddingPolynomial(struct Node *,struct Node *);
void MultiplyPolynomial(struct Node *,struct Node *);
int main( )
{
struct Node *Node1=NULL,*Node2=NULL;
printf("Polynomial 1 :\n");
Node1=create(Node1);
printf("Polynomail 2 :\n");
Node2=create(Node2);
printf("The Polynomial 1 is : ");
printingList(Node1);
printf("The Polynomial 2 is : ");
printingList(Node2);
AddingPolynomial(Node1, Node2);
MultiplyPolynomial(Node1, Node2);
}
struct Node *create(struct Node *start1)
{
int x1,n1,ex1;
float co1;
printf("The number of terms : ");
scanf("%d",&n1);
for(x1=1;x1<=n1;x1++)
{
printf("The coeficient for term %d : ",x1);
scanf("%f",&co1);
printf("Power for term %d : ",x1);
scanf("%d",&ex1);
start1=insert_s(start1,co1,ex1);
}
return start1;
}
struct Node *insert_s(struct Node *startNode,float coef,int ex)
{
struct Node *ptr,*temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->c=coef;
temp->power=ex;
if(startNode==NULL || ex > startNode->power)
{
temp->nextNode=startNode;
startNode=temp;
}
else
{
ptr=startNode;
while(ptr->nextNode!=NULL && ptr->nextNode->power >= ex)
ptr=ptr->nextNode;
temp->nextNode=ptr->nextNode;
ptr->nextNode=temp;
}
return startNode;
}
struct Node *insert(struct Node *startNode,float co,int ex)
{
struct Node *head,*temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->c=co;
temp->power=ex;
if(startNode==NULL)
{
temp->nextNode=startNode;
startNode=temp;
}
else
{
head=startNode;
while(head->nextNode!=NULL)
head=head->nextNode;
temp->nextNode=head->nextNode;
head->nextNode=temp;
}
return startNode;
}
//Displaying linked list
void printingList(struct Node *head)
{
if(head==NULL)
{
printf("Zero polynomial\n");
return;
}
while(head!=NULL)
{
printf("(%.1fx^%d)", head->c,head->power);
head=head->nextNode;
if(head!=NULL)
printf(" + ");
else
printf("\n");
}
}
void AddingPolynomial(struct Node *nodep1,struct Node *nodep2)
{
struct Node *startNode3;
startNode3=NULL;
while(nodep1!=NULL && nodep2!=NULL)
{
if(nodep1->power > nodep2->power)
{
startNode3=insert(startNode3,nodep1->c,nodep1->power);
nodep1=nodep1->nextNode;
}
else if(nodep2->power > nodep1->power)
{
startNode3=insert(startNode3,nodep2->c,nodep2->power);
nodep2=nodep2->nextNode;
}
else if(nodep1->power==nodep2->power)
{
startNode3=insert(startNode3,nodep1->c+nodep2->c,nodep1->power);
nodep1=nodep1->nextNode;
nodep2=nodep2->nextNode;
}
}
while(nodep1!=NULL)
{
startNode3=insert(startNode3,nodep1->c,nodep1->power);
nodep1=nodep1->nextNode;
}
while(nodep2!=NULL)
{
startNode3=insert(startNode3,nodep2->c,nodep2->power);
nodep2=nodep2->nextNode;
}
printf("Sum polynomial is : ");
printingList(startNode3);
}
void MultiplyPolynomial(struct Node *nodep1, struct Node *nodep2)
{
struct Node *startNode3;
struct Node *nodep2_beg = nodep2;
startNode3=NULL;
if(nodep1==NULL || nodep2==NULL)
{
printf("Zero polynomial\n");
return;
}
while(nodep1!=NULL)
{
nodep2=nodep2_beg;
while(nodep2!=NULL)
{
startNode3=insert_s(startNode3,nodep1->c*nodep2->c,nodep1->power+nodep2->power);
nodep2=nodep2->nextNode;
}
nodep1=nodep1->nextNode;
}
printf("Product polynomial is : ");
printingList(startNode3);
}
Comments
Leave a comment