Friday, November 23, 2012

c program to perform polynomial addition

polynomial addition using linked list

    #include<stdio.h>
    #include<malloc.h>
    #include<conio.h>
    struct link{
    int coeff;
    int pow;
    struct link *next;
    };
    struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
    void create(struct link *node)
    {
    char ch;
    do
    {
    printf("\n enter coeff:");
    scanf("%d",&node->coeff);
    printf("\n enter power:");
    scanf("%d",&node->pow);
    node->next=(struct link*)malloc(sizeof(struct link));
    node=node->next;
    node->next=NULL;
    printf("\n continue(y/n):");
    ch=getch();
    }
    while(ch=='y' || ch=='Y');
    }
    void show(struct link *node)
    {
    while(node->next!=NULL)
    {
    printf("%dx^%d",node->coeff,node->pow);
    node=node->next;
    if(node->next!=NULL)
    printf("+");
    }
    }
    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->pow<poly2->pow)
    {
    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;
    }
    }
    main()
    {
    char ch;
    do{
    poly1=(struct link *)malloc(sizeof(struct link));
    poly2=(struct link *)malloc(sizeof(struct link));
    poly=(struct link *)malloc(sizeof(struct link));
    printf("\nenter 1st number:");
    create(poly1);
    printf("\nenter 2nd number:");
    create(poly2);
    printf("\n1st Number:");
    show(poly1);
    printf("\n2nd Number:");
    show(poly2);
    polyadd(poly1,poly2,poly);
    printf("\nAdded polynomial:");
    show(poly);
    printf("\n add two more numbers:");
    ch=getch();
    }
    while(ch=='y' || ch=='Y');
    }
coefficients Like-power terms are added in polynomial addition.
i.e.;
First Polynomial Second Polynomial
coeffcient of x + coeffcient of x
coeffcient of x^2 + coeffcient of x^2
coeffcient of x^3 + coeffcient of x^3
--------------------------------------…
and so on. If first polynomial is of 5th degree and second is of 3rd degree.
Then u add terms of both polynomial upto 3rd term and u put remaining 2 terms (4th and 5th) of first polynomial as it is in the final polynomial.

So ur logic goes like this.
u will have three linked lists
first linked list for first polynomial
second linked list for second polynomial
third linked list for addition of polynomials


u traverse linked list of higher-degree polynomial and keep on adding coefficients of both polynomials in a third polynomial.
after adding each coefficient u move to next node in both the linked lists.
during traversal lower degree polynomial will end then u simply copy remaining terms in third linked list.


No comments:

Post a Comment