リンクリストを用いて2つの多項式を掛け算する

#処理内容
1)各リンクリストに対して、ノードを作成し、リストに追加します。但し、指数が同じノードに対して係数を足し算します。
2)二つのリンクリストのノードを比べて、指数が同じノードを掛け算し、新しいリストに追加します。また、片方にしか存在しないノードをそのまま新しいリストに追加します。

```multiply.c
#include <stdio.h>
#include <stdlib.h>

//node declaration
struct polynode{
  float coeff;
  int exp;
  struct polynode *link;
};

//function declaration
void poly_append(struct polynode **, float, int);
void poly_multiply(struct polynode *, struct polynode *, struct polynode **);
void display_poly(struct polynode *);

void main(){
  //declare pointer to three linked list
  struct polynode *first, *second, *mult;
  //declare variable for printing separator
  int i=1;
  
  //set pointer to empty list
  first = second = mult = NULL;
  
  //append node to first linked list
  poly_append(&first, 3, 5);
  poly_append(&first, 2, 4);
  poly_append(&first, 1, 2);
  
  //display first linked list
  display_poly(first);
  
  //append node to second linked list
  poly_append(&second, 1, 6);
  poly_append(&second, 2, 5);
  poly_append(&second, 3, 4);
  
  //display second linked list
  display_poly(second);
  
  //print separator
  while(i++<79)
    printf("-");
    
  printf("\n");
  
  //multiply first and second linked list into one
  poly_multiply(first, second, &mult);
  //display result
  display_poly(mult);
}

//multiply coefficient of nodes and append to new list if both lists are not empty.if one list is empty, copy the non empty list to a new list. if both lists are empty, do nothing.
void poly_multiply(struct polynode *f, struct polynode *s, struct polynode **m){
  //declare locator pointer
  struct polynode *q, *r;
  
  //if first and second list are empty, print message
  if(f == NULL && s == NULL){
    printf("Both lists are empty. Nothing to do.");
    //go back to main
    return;
  }
  
  //set locator pointer to non empty list. if both list is non empty, set locator to the first list
  q = (f == NULL ? s : f);
  
  //loop through non empty list
  while(q!=NULL){
    //append to new list
    poly_append(m,q->coeff,q->exp);
    //go to next node
    q = q->link;
  }
  
  //if both lists are not empty
  if(f != NULL && s != NULL){
    //set locator pointer to second list
    q = s;
    
    //loop through second list
    while(q!=NULL){
      //set another locator pointer to third list. reset to the top of third list at each loop through second list.
      r = *m;
      
      //loop through third list to find same exponential node
      while(r!=NULL){
        //if same exponential node is found
        if(r->exp == q->exp){
          //multiply coeffient
          r->coeff *= q->coeff;
          //jump out of loop
          break;
        }
        //go to next node(third list)
        r = r->link;
      }
      
      //if node not found in third list, add it to third list
      if(r == NULL){
        poly_append(m,q->coeff,q->exp);
      }
      
      //go to next node(second list)
      q = q->link;
    }
  }
  
}

//append node to linked list. add coefficient of the same exponential node without creating new node.
void poly_append(struct polynode **q, float c, int e){
  struct polynode *r, *temp;
  
  //if linked list is empty or given exponential is bigger than the first node.
  if(*q == NULL || (*q)->exp < e){
    //create new node
    r = malloc(sizeof(struct polynode));
    //set coefficient of new node
    r->coeff = c;
    //set exponential of new node
    r->exp = e;
    //set link of new node to NULL
    r->link = *q;
    //set pointer of linked list to the new node
    *q = r;
  }else if(e == (*q)->exp){//if given exponential is the same as the first node
    //add to coefficient of first node
    (*q)->coeff = (*q)->coeff + c;
  }
  else{//if linked list is not empty
    //set locator to the front of list
    temp = *q;
    //traverse the linked list
    while(temp!=NULL){
      //if given exponential is smaller than current node but bigger than the next node. if reach last node, check if given exponential is smaller than last node.
      if( (temp->exp > e) && ( (temp->link == NULL) || ((temp->link)->exp < e) ) ){
        //create new node
        r = malloc(sizeof(struct polynode));
        //set coefficient of new node
        r->coeff = c;
        //set exponential of new node
        r->exp = e;
        //set link of new node to the next node
        r->link = temp->link;
        //set link of current node to new node
        temp->link = r;
        //go back to main
        return;
      }else if(temp->exp == e){ //if exponential of current node is same with given exponential
        //add to coefficient of current node
        temp->coeff = temp->coeff + c;
        //go back to main
        return;
      }
      //go to next node
      temp = temp->link;
    }
  }
}

//display nodes in linked list
void display_poly(struct polynode *q){
  while(q!=NULL){
    printf("%fx^%d", q->coeff, q->exp);
    if(q->link!=NULL){
      printf("+");
    }
    q = q->link;
  }
  
  printf("\n");
}
```