title: Linked list polynomial multiplication
body: <p>I have written a program to multiply two polynomials in the form of single linked lists. I am unable to print output using this code.</p>
  
  <p>The output I am getting is</p>
  
  <pre class="lang-none prettyprint-override"><code>1st Number: 5x^2 + 4x^1 + 2x^0
  2nd Number: 5x^1 + 5x^0
  Multiplied polynomial:
  </code></pre>
  
  <p>How can I fix that?</p>
  
  <p>My code:</p>
  
  <pre><code>// C++ program for multiplication of two polynomials
  // using Linked Lists
  #include&lt;bits/stdc++.h&gt;
  using namespace std;
  
  // Node structure containing power and coefficient of variable
  struct node
  {
      int coeff;
      int exp;
      struct node *next;
  };
  void padd(float, int, node**);          
  // Function to create new node
  void create_node(int x, int y, struct node **temp)
  {
      struct node *r,*z ;
      z = *temp;
      if(z == NULL)
      {
          r =(struct node*)malloc(sizeof(struct node));
          r-&gt;coeff = x;
          r-&gt;exp = y;
          *temp = r;
          r-&gt;next = (struct node*)malloc(sizeof(struct node));
          r = r-&gt;next;
          r-&gt;next = NULL;
      }
      else
      {
          r-&gt;coeff = x;
          r-&gt;exp = y;
          r-&gt;next = (struct node*)malloc(sizeof(struct node));
          r = r-&gt;next;
          r-&gt;next = NULL;
      }
  }
  
  // Function Multiplying two polynomial numbers
  void polymul ( struct node *poly1, struct node *poly2,
                     struct node *poly )
  {
      struct node *y1 ;
      float coeff1;
      int exp1 ;
  
      y1 = poly2 ;  /* point to the starting of the second linked list */
  if ( poly1 == NULL &amp;&amp; poly2 == NULL )
          return ;
  
      /* if one of the list is empty */
  if ( poly1 == NULL )
          poly = poly2 ;
      else
      {
          if ( poly2 == NULL )
              poly = poly1 ;
          else/* if both linked lists exist */
  
          {
              /* for each term of the first list */
              while ( poly1 != NULL )
              {
                  /* multiply each term of the second linked list with a 
                      term of the first linked list */
                  while ( poly2 != NULL )
                  {
                      coeff1 = poly1 -&gt; coeff * poly2 -&gt; coeff ;
                      exp1 = poly1 -&gt; exp + poly2 -&gt; exp ;
                      poly2 = poly2 -&gt; next ;
  
                      /* add the new term to the resultant polynomial */
  
                      padd ( coeff1, exp1, &amp;poly ) ;
                  }
  
                  poly2 = y1 ;  /* reposition the pointer to the starting of
                           the second linked list */
  
  
                  poly1 = poly1 -&gt; next ;  /* go to the next node */
  
              }
          }
      }
  }
  
  /* adds a term to the polynomial in the descending order of the exponent */
  void padd ( float coeff, int exp, struct node **s )
  {
      struct node *r, *temp = *s ;
  
      /* if list is empty or if the node is to be inserted before the first node */
  if ( *s == NULL || exp &gt; ( *s ) -&gt; exp )
      {
          *s=r = (struct node*) malloc ( sizeof ( struct node ) ) ;
          (*s) -&gt; coeff = coeff ;
          (*s) -&gt; exp = exp ;
          (*s)-&gt; next = temp ;
      }
      else
      {
          /* traverse the entire linked list to search the position to insert a new node */
          while ( temp != NULL )
              {
                  if ( temp -&gt; exp == exp )
                  {
                      temp -&gt; coeff += coeff ;
                      return ;
                  }
  
                  if ( temp -&gt; exp &gt; exp &amp;&amp; ( temp -&gt; next -&gt; exp &lt; exp ||    temp -&gt; next == NULL ) )
                      {
                          r = (struct node* )malloc ( sizeof ( struct node ) ) ;
                          r -&gt; coeff = coeff;
                          r -&gt; exp = exp ;
                          r -&gt; next = temp -&gt; next ;
                          temp -&gt; next = r ;
                          return ;
                      }
  
              temp = temp -&gt; next ;  /* go to next node */
  
              }
  
          r -&gt; next = NULL ;
          temp -&gt; next = r ;
      }
  }
  
  // Display Linked list
  void show(struct node *node)
  {
  while(node-&gt;next != NULL)
      {
      printf("%dx^%d", node-&gt;coeff, node-&gt;exp);
      node = node-&gt;next;
      if(node-&gt;next != NULL)
          printf(" + ");
      }
  }
  
  // Driver program
  int main()
  {
      struct node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
  
      // Create first list of 5x^2 + 4x^1 + 2x^0
      create_node(5,2,&amp;poly1);
      create_node(4,1,&amp;poly1);
      create_node(2,0,&amp;poly1);
  
      // Create second list of 5x^1 + 5x^0
      create_node(5,1,&amp;poly2);
      create_node(5,0,&amp;poly2);
  
      printf("1st Number: "); 
      show(poly1);
  
      printf("\n2nd Number: ");
      show(poly2);
  
      poly = (struct node *)malloc(sizeof(struct node));
  
      // Function multiply two polynomial numbers
      polymul(poly1, poly2, poly);
  
      // Display resultant List
      printf("\nMultiplied polynomial: ");
      show(poly);
  
  return 0;
  }
  </code></pre>