polynomial addition

suggest one data structure for storing a sparse polynomial of the form a+ bx +cx^2 +...nx^n when n is large ?given two such polynomials write an algorithm to add them and store them in a third polynomial.

Comments

  1. void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
    {
    while(poly1->next && poly2->next)
    {
    if(poly1->pow>poly2->pow)
    {
    poly->pow=poly1->pow;
    poly->coeff=poly1->coeff;
    poly1=poly1->next;
    }
    else if(poly1->powpow)
    {
    poly->pow=poly2->pow;
    poly->coeff=poly2->coeff;
    poly2=poly2->next;
    }
    else
    {
    poly->pow=poly1->pow;
    poly->coeff=poly1->coeff+poly2->coeff;
    poly1=poly1->next;
    poly2=poly2->next;
    }
    poly->next=(struct link *)malloc(sizeof(struct link));
    poly=poly->next;
    poly->next=NULL;
    }
    while(poly1->next || poly2->next)
    {
    if(poly1->next)
    {
    poly->pow=poly1->pow;
    poly->coeff=poly1->coeff;
    poly1=poly1->next;
    }
    if(poly2->next)
    {
    poly->pow=poly2->pow;
    poly->coeff=poly2->coeff;
    poly2=poly2->next;
    }
    poly->next=(struct link *)malloc(sizeof(struct link));
    poly=poly->next;
    poly->next=NULL;
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog