Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node {
- int co;
- int exp;
- struct node *next;
- };
- struct node *PolynomailInput(int k) {
- struct node *p = (struct node*)malloc(sizeof(struct node));
- struct node *t = p;
- while(k) {
- t->next = (struct node*)malloc(sizeof(struct node));
- t= t->next;
- printf("Enter Coefficient and Exponent: ");
- scanf("%d %d", &t->co, &t-> exp);
- t->next = NULL;
- k--;
- }
- p=p->next;
- return p;
- }
- void display(struct node *p) {
- if(p == NULL) printf("Empty Linked List");
- else {
- struct node*x=p;
- while(x!=NULL) {
- printf("%dx^%d ", x->co, x->exp);
- x=x->next;
- }
- }
- }
- struct node *AddPolynomial(struct node *p, struct node*q) {
- struct node *add = (struct node*)malloc(sizeof(struct node));
- struct node *r = add;
- while(p!=NULL && q!=NULL) {
- r->next = (struct node*)malloc(sizeof(struct node));
- r=r->next;
- if(p->exp==q->exp) {
- r->co = p->co+q->co;
- r->exp = p->exp;
- r->next = NULL;
- p=p->next;
- q=q->next;
- }
- else if(q->exp>p->exp) {
- r->exp = q->exp;
- r->co=q->co;
- r->next = NULL;
- q=q->next;
- }
- else if(p->exp>q->exp) {
- r->exp = p->exp;
- r->co=p->co;
- r->next = NULL;
- p=p->next;
- }
- }
- while(p!=NULL) {
- r->next = (struct node*)malloc(sizeof(struct node));
- r=r->next;
- r->co = p->co;
- r->exp = p->exp;
- r->next = NULL;
- p=p->next;
- }
- while(q!=NULL) {
- r->next = (struct node*)malloc(sizeof(struct node));
- r=r->next;
- r->co = q->co;
- r->exp = q->exp;
- r->next = NULL;
- q=q->next;
- }
- add=add->next;
- return add;
- }
- struct node *SubPolynomial(struct node *p, struct node *q) {
- struct node *qtemp = (struct node*)malloc(sizeof(struct node));
- struct node *r = qtemp;
- while(q!=NULL) {
- r->next = (struct node*)malloc(sizeof(struct node));
- r=r->next;
- r->co = q->co * (-1);
- r->exp = q->exp;
- r->next = NULL;
- q=q->next;
- }
- qtemp=qtemp->next;
- return AddPolynomial(p,qtemp);
- }
- struct node *MulPolynomial(struct node *p1, struct node *p2) {
- struct node *p3 = NULL;
- struct node *q = p2;
- while(q!=NULL) {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- struct node *r = temp;
- struct node *p = p1;
- while(p!=NULL) {
- r->next = (struct node*)malloc(sizeof(struct node));
- r= r->next;
- r->co = p->co*q->co;
- r->exp = p->exp + q->exp;
- r->next = NULL;
- p=p->next;
- }
- temp=temp->next;
- p3=AddPolynomial(p3, temp);
- q=q->next;
- }
- return p3;
- }
- int main() {
- printf("Enter number of terms of first polynomial function: \n");
- int n;
- scanf("%d", &n);
- struct node* p1= PolynomailInput(n);
- printf("\nEnter number of terms of second polynomial function: \n");
- int m;
- scanf("%d", &m);
- struct node* p2= PolynomailInput(m);
- struct node* sum = AddPolynomial(p1,p2);
- struct node* diff = SubPolynomial(p1,p2);
- struct node* mul = MulPolynomial(p1,p2);
- printf("\nSum of polynomial functions: ");
- display(sum);
- printf("\nDifference of polynomial functions: ");
- display(diff);
- printf("\nMultiplication of polynomial functions: ");
- display(mul);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement