Polynomial Addition Using Linked List

chandrabhanu 0 Tallied Votes 49K Views Share

Above program takes input of coefficient and power separately of 2 different polynomials add them up to a new polynomial.It is successfully compiled and executed in DEV CPP as C file.It Turbo C compiler add void before main function to avoid warning messages.

sanket41 commented: Nice Logic appiled Behind This Snippet +0
#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');
}
sanket41 0 Newbie Poster

Hello,
this program will run successfully no doubt,but USER should enter the polynomials in ascending order only. This Program will not give Proper answer for DESCENDING order or any order that user might enter.........

svcj92 0 Newbie Poster

Hi,
This program is not working when we enter the polynomials in an unsorted order...

akashghuge 0 Newbie Poster
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int coef;
    int pow;
    struct node *next;
}poly;
menu()
{
    int ch;
    system("clear");
    printf("\n\t1)Create 1'st Polynomial ");
    printf("\n\t2)Create 2'nd Polynomial ");
    printf("\n\t3)Display");
    printf("\n\t4)Add Polynomials");
    printf("\n\tEnter your choice:");
    scanf("%d",&ch);
    return ch;
}
void b_sort(poly *head)
{
    poly *t1,*temp;
    int i,flag=1,scan;
    t1=(poly *)malloc(sizeof(poly));
    temp=(poly *)malloc(sizeof(poly));
    for(scan=1;flag==1;scan++)
    {
        temp=head->next;
        flag=0;
        for(i=0;i<head->coef-scan;i++)
        {
            if(temp->pow<temp->next->pow)
            {
                t1->coef=temp->coef;
                t1->pow=temp->pow;

                temp->coef=temp->next->coef;
                temp->pow=temp->next->pow;

                temp->next->coef=t1->coef;
                temp->next->pow=t1->pow;

                flag=1;
            }
            temp=temp->next;
        }
    }
}
void create(poly **head)
{
    int coef,pow;
    *head=(poly *)malloc(sizeof(poly));
    printf("\n\tEnter number of terms: ");
    scanf("%d",&(*head)->coef);
    (*head)->next=NULL;
    int i;
    poly *newnode;
    poly  *curr=*head;
    for(i=0;i<(*head)->coef;i++)
    {
        newnode=(poly *)malloc(sizeof(poly));
        system("clear");
        printf("\n\tEnter %d term: ",i+1);
        scanf("%d",&coef);
        printf("\n\tEnter power of %d term: ",i+1);
        scanf("%d",&pow);
        newnode->coef=coef;
        newnode->pow=pow;
        while(curr->next)
            curr=curr->next;
        curr->next=newnode;
        curr=newnode;
    }
    curr->next=NULL;
}
void display(poly *head)
{
    if(head==NULL)
        printf("\n\tEmpty\n");
    else
    {
        poly *temp=head->next;
        while(temp)
        {
            if(temp->next==NULL)
            {
                if(temp->pow==0)
                {
                    printf("%d",temp->coef);
                    return;
                }
                if(temp->pow==1)
                {
                    printf("%dx",temp->coef);
                    return;
                }
                else
                {
                    printf("%dx^%d",temp->coef,temp->pow);
                    return;
                }
            }
            else
            if(temp->pow==1)
                printf("%dx+",temp->coef);
            else
            if(temp->pow==0)
                printf("%d+",temp->coef);
            else
                printf("%dx^%d+",temp->coef,temp->pow);
            temp=temp->next;
        }
    }
}
void adddisplay(poly *head)
{
    if(head==NULL)
        printf("\n\tEmpty\n");
    else
    {
        poly *temp=head->next->next;
        while(temp)
        {
            if(temp->next==NULL)
            {
                if(temp->pow==0)
                {
                    printf("%d",temp->coef);
                    return;
                }
                if(temp->pow==1)
                {
                    printf("%dx",temp->coef);
                    return;
                }
                else
                {
                    printf("%dx^%d",temp->coef,temp->pow);
                    return;
                }
            }
            else
            if(temp->pow==1)
                printf("%dx+",temp->coef);
            else
            if(temp->pow==0)
                printf("%d+",temp->coef);
            else
                printf("%dx^%d+",temp->coef,temp->pow);
            temp=temp->next;
        }
    }
}
poly *add(poly *head1,poly *head2)
{
    poly *head3=NULL;
    poly *curr1,*curr2,*curr3;
    curr1=head1; curr2=head2; curr3=head3;
    poly *newnode;
    head3=(poly *)malloc(sizeof(poly));
    curr3=head3;
    while(curr1||curr2)
    {
        newnode=(poly *)malloc(sizeof(poly));
        newnode->next=NULL;
        if(curr1->pow>curr2->pow)
        {
            newnode->coef=curr1->coef;
            newnode->pow=curr1->pow;
            curr1=curr1->next;
        }
        else
        if(curr1->pow<curr2->pow)
        {
            newnode->coef=curr2->coef;
            newnode->pow=curr2->pow;
            curr2=curr2->next;
        }
        else
        {
            newnode->coef=curr1->coef+curr2->coef;
            newnode->pow=curr2->pow;
            curr1=curr1->next;
            curr2=curr2->next;
        }
        if(curr1==NULL&&curr2!=NULL)
        {
            newnode->coef=curr2->coef;
            newnode->pow=curr2->pow;
            curr2=curr2->next;      
        }
        if(curr2==NULL&&curr1!=NULL)
        {
            newnode->coef=curr1->coef;
            newnode->pow=curr1->pow;
            curr1=curr1->next;
        }
        curr3->next=newnode;
        curr3=newnode;
        curr3->next=NULL;
    }
    return head3;   
}
main()
{
    int ch;
    poly *head1=NULL;
    poly *head2=NULL;
    while((ch=menu())!=0)
    {
        if(ch==1)
            create(&head1);
        else
        if(ch==2)
            create(&head2);
        else
        if(ch==3)
        {
            printf("\n\t1'st Polynomial\t\t");      
            display(head1);
            printf("\n");
            printf("\n\t2'nd Polynomial\t\t");
            display(head2);
            getchar();getchar();
        }
        else
        if(ch==4)
        {
            poly *head3=NULL;
            b_sort(head1);
            b_sort(head2);
            head3=add(head1,head2);
            printf("\n\tResult\t\t");
            adddisplay(head3);
            getchar();getchar();
        }
    }
}
akashghuge 0 Newbie Poster

this works for un-sorted data..

Kulbir_1 0 Newbie Poster

More Optimized Solution which covers all the cases:

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int coef,expo;
    struct node* next;
};

struct node* insertpoly(struct node* thead,int c,int e);
struct node* append(struct node* thead,int c,int e);
struct node* polyaddition(struct node* p1thead,struct node* p2thead);
void display(struct node* thead);

void main()
{
    int a,b,n,i;
    struct node* p1head,* p2head,* p3head;
    p1head=p2head=NULL;

    // Inputing the first polynomial..

    printf("Enter the no of terms of polynomial 1..");
    scanf("%d",&n);
    printf("\nEnter the polynomial..");
    for(i=0;i<n;i++){
        printf("\nEnter the coefficient and exponent of the term..");
        scanf("%d%d",&a,&b);
        p1head=insertpoly(p1head,a,b);
    }

    // Inputing the second polynomial..

    printf("\nEnter the no of terms of polynomial 2..");
    scanf("%d",&n);
    printf("\nEnter the polynomial..");
    for(i=0;i<n;i++){
        printf("\nEnter the coefficient and exponent of the term..");
        scanf("%d%d",&a,&b);
        p2head=insertpoly(p2head,a,b);
    }

    //Performing Addition..

    p3head=polyaddition(p1head,p2head);

    //Displaying the polynomial..

    printf("\nThe polynomial 1 is..");
    display(p1head);
    printf("\nThe polynomial 2 is..");
    display(p2head);
    printf("\nThe sum of the two polynomials is..");
    display(p3head);
}

struct node* append(struct node* thead,int c,int e)
{
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    newnode->coef=c;
    newnode->expo=e;
    if(thead==NULL){// Corner Case to handle if the list is empty...
    newnode->next=NULL;
    return newnode;
    }
    struct node* trav=thead;
    while(trav->next!=NULL) // Traversing to point to the last node...
        trav=trav->next;
    trav->next=newnode;
    newnode->next=NULL;
    return thead;
}

struct node* insertpoly(struct node* thead,int c,int e)
{
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    newnode->coef=c;
    newnode->expo=e;
    if(thead==NULL){            // for inserting the first node..
        newnode->next=NULL;
        return newnode;
    }
    struct node* prev,* curr;
    prev=curr=thead;
    while(curr!=NULL && curr->expo>e){
        prev=curr;
        curr=curr->next;
    }
    if(curr==thead){            // for inserting before the first node...
        newnode->next=curr;
        return newnode;
    }
    else if(curr==NULL){        //for inserting after the last node....
        prev->next=newnode;
        newnode->next=NULL;
    }
    else{
        newnode->next=curr;
        prev->next=newnode;
    }
    return thead;
}

struct node* polyaddition(struct node* p1thead,struct node* p2thead)
{
    struct node* ans=NULL;
    struct node* t1,* t2;
    t1=p1thead;
    t2=p2thead;
    while(t1!=NULL && t2!=NULL){
        if(t1->expo > t2->expo){
            ans=append(ans,t1->coef,t1->expo);
            t1=t1->next;
        }
        else if(t1->expo < t2->expo){
            ans=append(ans,t2->coef,t2->expo);
            t2=t2->next;
        }
        else{
            ans=append(ans,(t1->coef)+(t2->coef),t1->expo);
            t1=t1->next;
            t2=t2->next;
        }
    }

    while(t1!=NULL){            //coping the remaining terms of polynomial 1...
        ans=append(ans,t1->coef,t1->expo);
        t1=t1->next;
    }

    while(t2!=NULL){            //coping the remaining terms of polynomial 2...
        ans=append(ans,t2->coef,t2->expo);
        t2=t2->next;
    }
    return ans;
}

void display(struct node* thead)
{
    struct node* temp=thead;
    if(temp==NULL){
        printf("\nEmpty..");
    }
    else{
        while(temp->next!=NULL){
            printf(" %dx^%d +",temp->coef,temp->expo);
            temp=temp->next;
        }
       printf(" %dx^%d ",temp->coef,temp->expo);
    }
}
Ankush_Ghosh 0 Newbie Poster

`

#include<stdio.h>
#include<stdlib.h>

typedef struct node {
    int coeff,expo;
    struct node *next;
}N;

N *input();
N *p_addition(N *,N *);
N *getnode();

N *getnode() {
    N *new;
    new=(N*)malloc(sizeof(N));      
    new->next=NULL;
}
N *p_addition(N *ptr,N *qtr) {
    N *rptr=getnode(),*r1ptr=rptr,*aptr=NULL;
    int s;
    printf("\n...................addition starts...................\n");
    while(ptr!=NULL&&qtr!=NULL) {
        if(ptr->expo==qtr->expo) {
            s=ptr->coeff+qtr->coeff;
            if(s!=0) {
                aptr=getnode();
                aptr->coeff=s;
                aptr->expo=ptr->expo;
                r1ptr->next=aptr;
                r1ptr=aptr;
            }           
            ptr=ptr->next;
            qtr=qtr->next;
        }

        else if(ptr->expo>qtr->expo) {
            aptr=getnode();
            aptr->coeff=ptr->coeff;
            aptr->expo=ptr->expo;
            ptr=ptr->next;
            r1ptr->next=aptr;
            r1ptr=aptr;
        }
        else {
            aptr=getnode();
            aptr->coeff=qtr->coeff;
            aptr->expo=qtr->expo;
            qtr=qtr->next;
            r1ptr->next=aptr;
            r1ptr=aptr;

        }
    }
        while(ptr!=NULL) {
            aptr=getnode();
            aptr->coeff=ptr->coeff;
            aptr->expo=ptr->expo;
            ptr=ptr->next;
            r1ptr->next=aptr;
            r1ptr=aptr;

        }
        while(qtr!=NULL) {
            aptr=getnode();
            aptr->coeff=qtr->coeff;
            aptr->expo=qtr->expo;
            qtr=qtr->next;
            r1ptr->next=aptr;
            r1ptr=aptr;
        }       
        return (rptr->next);
}
N *input()
{
    N *new,*qtr,*ptr;
    int i,n;
    new=(N *)malloc(sizeof(N));
    new->next=NULL;
    qtr=new;
    printf("Enter number of nodes");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        ptr=(N*)malloc(sizeof(N));      
        printf("Enter Coeff & Expo respectively");
        scanf("%d%d",&ptr->coeff,&ptr->expo);
        ptr->next=NULL;
        qtr->next=ptr;
        qtr=qtr->next;
    }
    return(new->next);
}
int main() 
{
    N *ptr,*qtr,*add;
    ptr=input();
    qtr=input();
    add=p_addition(ptr,qtr);
    while(add!=NULL) {
        printf("%dX^%d+",add->coeff,add->expo);
        add=add->next;
    }

}

`

satish_12 0 Newbie Poster

does this program based on doubly linked list ???

Nithesh_1 0 Newbie Poster
  //THIS CODE WORKS FOR UNSORTED DATA.DATA IS SORTED FOR A GIVEN POLYNOMIAL EXPRESSION..
 #include<stdio.h> 
   #include<stdlib.h>
 struct link{
   int coeff;
   int pow;
   struct link *next;
   };
 struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
 int n,i;
printf("enter number of terms\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
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;
}
} 
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;
   }
    }
   void sort(struct link*node)
   {
struct link *i,*j;
int t;
for(i=node;i->next!=NULL;i=i->next)
{
    for(j=i->next;j!=NULL;j=j->next)
    {
        if(i->pow<=j->pow)
        {
            t=i->pow;
            i->pow=j->pow;
            j->pow=t;
            t=i->coeff;
            i->coeff=j->coeff;
            j->coeff=t;
        }
    }
}
 }
main()
{
  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);
  sort(poly1);
  printf("\nenter 2nd number:");
  create(poly2);
  sort(poly2);
  printf("\n1st Number:");
  show(poly1);
  printf("\n2nd Number:");
  show(poly2);
  polyadd(poly1,poly2,poly);
  printf("\nAdded polynomial:");
  show(poly);
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.