title: Polynomial multiplication using linked list in c
body: <p>Iam working on a program to perform addition,subtraction,multiplication and differentiation operations on a polynomial using linked list in c.
  The other operations are working fine except multiplication one.
  here is the code</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;malloc.h&gt;
  #include&lt;conio.h&gt;
  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\nenter coeff:");
    scanf("%d",&amp;node-&gt;coeff);
    printf("\nenter power:");
    scanf("%d",&amp;node-&gt;pow);
    node-&gt;next=(struct link*)malloc(sizeof(struct link));
    node=node-&gt;next;
    node-&gt;next=NULL;
  printf("\ncontinue(y/n):");
  ch=getch();
  }
  while(ch=='y' || ch=='Y');
  }
  void show(struct link *node)
  {
  while(node-&gt;next!=NULL)
  {
  
  printf("%dx^%d",node-&gt;coeff,node-&gt;pow);
  node=node-&gt;next;
  if(node-&gt;next!=NULL)
  printf(" + ");
  
  
  }
  }
  void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
  {
   while(poly1-&gt;next &amp;&amp;  poly2-&gt;next)
   {
    if(poly1-&gt;pow&gt;poly2-&gt;pow)
    {
     poly-&gt;pow=poly1-&gt;pow;
     poly-&gt;coeff=poly1-&gt;coeff;
     poly1=poly1-&gt;next;
     }
    else if(poly1-&gt;pow&lt;poly2-&gt;pow)
    {
     poly-&gt;pow=poly2-&gt;pow;
     poly-&gt;coeff=poly2-&gt;coeff;
     poly2=poly2-&gt;next;
     }
    else
    {
     poly-&gt;pow=poly1-&gt;pow;
     poly-&gt;coeff=poly1-&gt;coeff+poly2-&gt;coeff;
     poly1=poly1-&gt;next;
     poly2=poly2-&gt;next;
     }
    poly-&gt;next=(struct link *)malloc(sizeof(struct link));
    poly=poly-&gt;next;
    poly-&gt;next=NULL;
   }
   while(poly1-&gt;next || poly2-&gt;next)
   {
    if(poly1-&gt;next)
    {
     poly-&gt;pow=poly1-&gt;pow;
     poly-&gt;coeff=poly1-&gt;coeff;
     poly1=poly1-&gt;next;
     }
    if(poly2-&gt;next)
    {
     poly-&gt;pow=poly2-&gt;pow;
     poly-&gt;coeff=poly2-&gt;coeff;
     poly2=poly2-&gt;next;
     }
     poly-&gt;next=(struct link *)malloc(sizeof(struct link));
     poly=poly-&gt;next;
     poly-&gt;next=NULL;
     }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>void polysub(struct link *poly1,struct link *poly2,struct link *poly)
  {
   while(poly1-&gt;next &amp;&amp;  poly2-&gt;next)
   {
    if(poly1-&gt;pow&gt;poly2-&gt;pow)
    {
     poly-&gt;pow=poly1-&gt;pow;
     poly-&gt;coeff=poly1-&gt;coeff;
     poly1=poly1-&gt;next;
     }
    else if(poly1-&gt;pow&lt;poly2-&gt;pow)
    {
     poly-&gt;pow=poly2-&gt;pow;
     poly-&gt;coeff=poly2-&gt;coeff;
     poly2=poly2-&gt;next;
     }
    else
    {
     poly-&gt;pow=poly1-&gt;pow;
     poly-&gt;coeff=poly1-&gt;coeff-poly2-&gt;coeff;
     poly1=poly1-&gt;next;
     poly2=poly2-&gt;next;
     }
    poly-&gt;next=(struct link *)malloc(sizeof(struct link));
    poly=poly-&gt;next;
    poly-&gt;next=NULL;
   }
   while(poly1-&gt;next || poly2-&gt;next)
   {
    if(poly1-&gt;next)
    {
     poly-&gt;pow=poly1-&gt;pow;
     poly-&gt;coeff=poly1-&gt;coeff;
     poly1=poly1-&gt;next;
     }
    if(poly2-&gt;next)
    {
     poly-&gt;pow=poly2-&gt;pow;
     poly-&gt;coeff=poly2-&gt;coeff;
     poly2=poly2-&gt;next;
     }
     poly-&gt;next=(struct link *)malloc(sizeof(struct link));
     poly=poly-&gt;next;
     poly-&gt;next=NULL;
     }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>void polymul(struct link *n1, struct link *n2, struct link *n)
  {
      struct link * n2beg=n2;
  
              while (n1) 
              {
                      struct link * temp=(struct link *)malloc(sizeof(struct link));
                      temp-&gt;next=NULL;    
                      n2=n2beg;
                      while (n2) 
                      {
                              temp-&gt;coeff = n1-&gt;coeff * n2-&gt;coeff;
  
                              temp-&gt;pow = n1-&gt;pow + n2-&gt;pow;
  
                              n2 = n2-&gt;next;
                              temp-&gt;next=(struct link *)malloc(sizeof(struct link));
                              temp=temp-&gt;next;
                              temp-&gt;next=NULL;
  
                      }
  
                      polyadd(temp,n,n);
                      n1 = n1-&gt;next;
                      free(temp);
              }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>void diff(struct link* p1,struct link* p2)
  {
  
  while(p1-&gt;next!=NULL)
  {
      p2-&gt;coeff=p1-&gt;coeff*p1-&gt;pow;
      p2-&gt;pow=p1-&gt;pow-1;
      p2-&gt;next=NULL;
      p2-&gt;next=(struct link *)malloc(sizeof(struct link));
     p2=p2-&gt;next;
     p2-&gt;next=NULL;
     p1=p1-&gt;next;
  }
  
  }
    main()
    {
    int op;
    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("\n\nWhat do you want to do?\n1.Addition\n2.Subtraction
            \n3.Multiplication\n4.Differentiation\n0.Exit
            \nEnter your choice:");
    scanf("%d",&amp;op);
    switch(op)
    {
          case 1:
              printf("\n\nenter 1st polynomial:");
              create(poly1);
              printf("\n\nenter 2nd polynomial:");
              create(poly2);
              printf("\n1st Polynomial:\t");
              show(poly1);
              printf("\n2nd Polynomial:\t");
              show(poly2);
              polyadd(poly1,poly2,poly);
              printf("\nAdded polynomial:\t");
              show(poly);
              break;
          case 2:
              printf("\n\nenter 1st polynomial:\t");
              create(poly1);
              printf("\n\nenter 2nd polynomial:\t");
              create(poly2);
              printf("\n\n1st Polynomial:\t");
              show(poly1);
              printf("\n\n2nd Polynomial:\t");
              show(poly2);
              polysub(poly1,poly2,poly);
              printf("\n\nSubtracted polynomial:\t");
              show(poly);
              break;  
          case 3:
              printf("\n\nenter 1st polynomial:");
              create(poly1);
              printf("\n\nenter 2nd polynomial:");
              create(poly2);
              printf("\n\n1st Polynomial:\t");
              show(poly1);
              printf("\n\n2nd Polynomial:\t");
              show(poly2);
              polymul(poly1,poly2,poly);
              printf("\n\nMultiplied polynomial:\t");
              show(poly);
              break;
          case 4:
              printf("\n\nenter polynomial:");
              create(poly1);
              printf("\n\nPolynomial:\t");
              show(poly1);
              diff(poly1,poly2);
              printf("\n\nDifferentiated Polynomial:\t");
              show(poly2);
              break;
          }
  
   /* printf("\n Want to continue? Y/N:");
    ch=getch();*/
    }
    while(op);
  </code></pre>
  
  <p>}</p>