Answer to Question #229332 in C for Arup

Question #229332

Polynomial addition and multiplication using linked lists.



1
Expert's answer
2021-08-25T18:30:51-0400
#include<stdio.h>
#include<stdlib.h>


struct LinkedList
{


float c;
int pow;
struct LinkedList *next;
 
};


struct LinkedList *create(struct LinkedList *);
struct LinkedList *insert_s(struct LinkedList *,float,int);
struct LinkedList *insert(struct LinkedList *,float,int);
void print(struct LinkedList *ptr);
void polyAdding(struct LinkedList *,struct LinkedList *);
void polyMultiply(struct LinkedList *,struct LinkedList *);


int main( )
{
 
 struct LinkedList *startNode1=NULL,*startNode2=NULL;
 
 printf("Polynomial 1 :\n");
 startNode1=create(startNode1);
 
 printf("Polynomail 2 :\n");
 startNode2=create(startNode2);
 
 printf("The Polynomial 1 is : ");
print(startNode1);
 
 printf("The Polynomial 2 is : ");
 print(startNode2);
 
 polyAdding(startNode1, startNode2);
 
polyMultiply(startNode1, startNode2);
 
}


//Creating a linked list
struct LinkedList *create(struct LinkedList *startNode)
{
 
 int x,n,ex;
 float co;
 
 printf("Number of terms : ");
 scanf("%d",&n);
 
 for(x=1;x<=n;x++)
 {
 
 printf("Coeficient for term %d : ",x);
 scanf("%f",&co);
 
 printf("Power for term %d : ",x);
 scanf("%d",&ex);
 startNode=insert_s(startNode,co,ex);
 
}
 
 return startNode;
 
}




struct LinkedList *insert_s(struct LinkedList *start,float coef,int ex)
{
 
 struct LinkedList *ptr,*temp;
 
 temp=(struct LinkedList *)malloc(sizeof(struct LinkedList));
 temp->c=coef;
 temp->pow=ex;




 if(start==NULL || ex > start->pow)
 {
 
 temp->next=start;
 start=temp;
 
 }
 else
 {
 
 ptr=start;
 
 while(ptr->next!=NULL && ptr->next->pow >= ex)
 ptr=ptr->next;
 temp->next=ptr->next;
 ptr->next=temp;
 
}
 
 return start;
 
}




struct LinkedList *insert(struct LinkedList *startNode,float co,int ex)
{
 struct LinkedList *head,*temp;
 
 temp=(struct LinkedList *)malloc(sizeof(struct LinkedList));
 temp->c=co;
 temp->pow=ex;
 
 //If there is no element in a linked list
 if(startNode==NULL)
 {
 
 temp->next=startNode;
 startNode=temp;
 
 }
 else 
 {
 
 head=startNode;
 
 while(head->next!=NULL)
 head=head->next;
 temp->next=head->next;
 head->next=temp;
 }
 return startNode;
}


//Printing linked list
void print(struct LinkedList *head)
{
 
 if(head==NULL)
 {
 
 printf("Zero polynomial\n");
 return;
 
 }
 
 while(head!=NULL)
 {
 
 printf("(%.1fx^%d)", head->c,head->pow);
 head=head->next;
 
 if(head!=NULL)
 printf(" + ");
 
 else
 printf("\n");
 }
 
}










 
void polyAdding(struct LinkedList *nodep1,struct LinkedList *nodep2)
{
 
 struct LinkedList *startNode3;
 startNode3=NULL;
 
 while(nodep1!=NULL && nodep2!=NULL)
 {
 
 if(nodep1->pow > nodep2->pow)
 {
 startNode3=insert(startNode3,nodep1->c,nodep1->pow);
 nodep1=nodep1->next;
 }
 
 else if(nodep2->pow > nodep1->pow)
 {
 startNode3=insert(startNode3,nodep2->c,nodep2->pow);
 nodep2=nodep2->next;
 }
 
 else if(nodep1->pow==nodep2->pow)
 {
 startNode3=insert(startNode3,nodep1->c+nodep2->c,nodep1->pow);
 nodep1=nodep1->next;
 nodep2=nodep2->next;
 }
 }
 


 while(nodep1!=NULL)
 {
 startNode3=insert(startNode3,nodep1->c,nodep1->pow);
 nodep1=nodep1->next;
 }
 


 while(nodep2!=NULL)
 {
 startNode3=insert(startNode3,nodep2->c,nodep2->pow);
 nodep2=nodep2->next;
 }
 
 printf("Sum polynomial is : ");
print(startNode3);
}


void polyMultiply(struct LinkedList *nodep1, struct LinkedList *nodep2)
{
 struct LinkedList *startNode3;
 struct LinkedList *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->pow+nodep2->pow);
 nodep2=nodep2->next;
 }
 
 nodep1=nodep1->next;
 
 }
 
 printf("Product polynomial is : ");
 print(startNode3);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS