Thursday, April 30, 2015

Sum of two polynomial: Implementation using linked-list.

#include<alloc.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>


struct poly
{
    int deg;
    int constant;
    struct poly *next;
};

void create_poly(int, int, struct poly**);
void disp_poly(struct poly*);
void sum_poly(struct poly*, struct poly*, struct poly**);


void main()
{
    struct poly *P1,*P2,*SUM_POLY;
   int degree,constant;
   clrscr();
    P1=P2=SUM_POLY=NULL;
    printf("\n\tPOLYNOMIAL 1 construction :: \n");
    printf("\nEnter degree of polynomial 1 : ");
   scanf("%d",&degree);
    if(degree==0)
        goto NEXTP1;
    while(degree>=0)
    {
        printf("\nEnter constant for degree %d : ",degree);
       scanf("%d",&constant);
        if(constant==0)
        {
            degree--;
            continue;
        }
        else
            create_poly(degree,constant,&P1);
        degree--;
   }
    NEXTP1:
    printf("\n\tPOLYNOMIAL 2 construction :: \n");
    printf("\nEnter degree of polynomial 2 : ");
   scanf("%d",&degree);
   if(degree==0)
        goto NEXTP2;
   while(degree>=0)
    {
        printf("\nEnter constant for degree %d : ",degree);
        scanf("%d",&constant);
        if(constant==0)
        {
            degree--;
            continue;
        }
        else
            create_poly(degree,constant,&P2);
        degree--;
   }
    NEXTP2:
   sum_poly(P1,P2,&SUM_POLY);
      printf("\nPolynomial 1 ==>\n\t");
    disp_poly(P1);
   printf("\nPolynomial 2 ==>\n\t");
    disp_poly(P2);
    printf("\nSum of Polynomials is ==>\n\t");
    disp_poly(SUM_POLY);
    getch();
}

void create_poly(int d, int c, struct poly **q)
{
    struct poly *temp;
    temp=*q;
    if(temp==NULL)
    {
        temp=*q=(struct poly*)malloc(sizeof(struct poly));
    }
    else
    {
        while(temp->next!=NULL)
            temp=temp->next;
        temp->next=(struct poly*)malloc(sizeof(struct poly));
        temp=temp->next;
    }
    temp->deg=d;
    temp->constant=c;
      temp->next=NULL;
}

void disp_poly(struct poly *q)
{
    struct poly *temp;
    temp=q;
    while(temp!=NULL)
    {
        printf("%dx^%d",abs(temp->constant),temp->deg);
        if(temp->next!=NULL)
            if((temp->next)->constant>=0)
                printf(" + ");
            else
                printf(" - ");
        temp=temp->next;
    }
}

//***********************************************
//sum_poly() function for adding two polynomials.
//***********************************************

void sum_poly(struct poly *p1, struct poly *p2, struct poly **sum_poly)
{
    struct poly *t1,*t2,*sum;
    int count=1;
    t1=p1;
    t2=p2;
    if(t1==NULL && t2==NULL)
    {
        printf("Cannot merge polynomials of order 0");
        getch();
        exit(0);
   }
    if(t1==NULL)
    {
        *sum_poly=t2;
        goto LAST;
   }
    if(t2==NULL)
    {
        *sum_poly=t1;
        goto LAST;
   }
    sum=*sum_poly;
    while(t1!=NULL || t2!=NULL)
    {
            if(t1==NULL)
            {
                sum->next=(struct poly*)malloc(sizeof(struct poly));
                sum=sum->next;
            sum->constant=t2->constant;
                sum->deg=t2->deg;
                sum->next=NULL;
                t2=t2->next;
            }
            else
         if(t2==NULL)
            {
                sum->next=(struct poly*)malloc(sizeof(struct poly));
                sum=sum->next;
            sum->constant=t1->constant;
                sum->deg=t1->deg;
                sum->next=NULL;
                t1=t1->next;
            }
            else
            if(t1->deg==t2->deg)

            {
                if(sum==NULL)
                {
                *sum_poly=(struct poly*)malloc(sizeof(struct poly));
                sum=*sum_poly;
                }
                else
                {
                    sum->next=(struct poly*)malloc(sizeof(struct poly));
                 sum=sum->next;
                }
                sum->constant=t1->constant+t2->constant;
              sum->deg=t1->deg;
            sum->next=NULL;
                t1=t1->next;
                t2=t2->next;
                if(count==1)
                    *sum_poly=sum;
         }
            else
              if(t1->deg>t2->deg)
           {
                sum->next=(struct poly*)malloc(sizeof(struct poly));
              sum=sum->next;
            sum->constant=t1->constant;
            sum->deg=t1->deg;
               sum->next=NULL;
                t1=t1->next;
                if(count==1)
                    *sum_poly=sum;
             }
             else
              {
                 sum->next=(struct poly*)malloc(sizeof(struct poly));
                 sum=sum->next;
                sum->constant=t2->constant;
             sum->deg=t2->deg;
                sum->next=NULL;
                 t2=t2->next;
                 if(count==1)
                    *sum_poly=sum;
             }
           count++;
     }//wend
LAST:
}

No comments:

Post a Comment