Untitled
plain_text
a month ago
1.5 kB
1
Indexable
Never
#include <stdio.h> #include <stdlib.h> struct node{ int coeff; int exp; struct node* next }; struct node*insert(node*head , int coeff , int exp){ struct node * new term = (node*)malloc(sizeof(node)); new term -> coeff = coeff; new term -> exp = exp; new term -. next = NULL; if (head == NULL) { return new term ; } struct node * curr=head; while (curr->next!=NULL) { curr = curr->next; } curr->next = new term; return head; } struct node * multipoly(node* poly1 , node* poly2) { node * result = NULL; for (node* term1= poly1 ; term1! = NULL ; term1 = term1->next) { for (node* term2=poly2 ; term2! = NULL = ; term2 = term2->next){ int coeff = term1->coeff * term2->coeff; int exp= term1->exp+term2->exp; result = insert(result , coeff , exp); } } return result ; } void display(node*poly) { struct node curr = poly; while (curr!= NULL){ printf("(%d)x^%d", curr ->coeff , curr->exp); curr= curr->next; if (curr!= NULL) { printf("+"); } } } int main () { struct node * poly1= NULL; struct node * poly2= NULL; poly1 = insert(poly1,4,3); poly1 = insert(poly1,7,1); poly1 = insert(poly1,5,0); poly2= insert(poly2,5,3); poly2= insert(poly2,3,2); poly2= insert(poly2,1,0); display(poly1); display(poly2); struct node * result = multipoly(poly1,poly2); display(result); return 0; }