リンクリストで多項式を作成

#コードの説明
1)作成されるノード内に係数と指数の情報を保持する変数があります。
2)ノードをリンクリストに追加する関数(poly_append)を使用する時に、同じ指数を持つノードがあれば、そのノードの係数に提示した係数を足します。リスト内に提示した指数を持つノードがなければ、新しいノードが作成され、リストに追加されます。
3)二つのリンクリストのノードを利用して、指数が重複しない新しいリンクリストを作成する関数はpoly_additionとなります。同じ指数を持つノードがあれば、係数を既存のノードに足し算します。

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

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

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

void main(){
  //declare pointer to point to three linked list
  struct polynode *first, *second, *total;
  //declare variable for loop
  int i = 0;
  
  //set all pointer to empty list
  first = second = total = NULL;
  
  //append node to the first linked list
  poly_append(&first,1.4,5);
  poly_append(&first,1.5,4);
  poly_append(&first,1.7,2);
  poly_append(&first,1.8,1);
  poly_append(&first,1.9,0);
  
  //display nodes of first linked list
  display_poly(first);
  
  //append node to the second linked list
  poly_append(&second,1.5,6);
  poly_append(&second,2.5,5);
  poly_append(&second,-3.5,4);
  poly_append(&second,4.5,3);
  poly_append(&second,6.5,1);
  
  //display nodes of second linked list
  display_poly(second);
  
  //print separator between first, second linked list and target linked list
  while(i++<79)
    printf("-");
  printf("\n");
  
  //add first and second linked list to target linked list.(add coefficient of same exponential)
  poly_addition(first,second,&total);
  //display nodes of target linked list
  display_poly(total);
  
}

//add two linked list into one. add coefficient of same exponential node.
void poly_addition(struct polynode *q, struct polynode *r, struct polynode **s){
  struct polynode *temp, *t;
  
  //if both list are empty, output message and return to main
  if(q == NULL && r == NULL){
    printf("Both list are empty. Nothing to do.\n");
    return;
  }
  
  //set locator to none empty list. if both list is not empty, set to the first list
  temp = (q == NULL ? r : q);
  
  //loop through none empty list
  while(temp!=NULL){
     //copy none empty list to the target list
     poly_append(s, temp->coeff, temp->exp);
     temp = temp->link;
  }
  
  //if both list is no empty, copy the second list to target list
  if(q != NULL && r != NULL){
    //set locator to the second list
    temp = r;
    
    //loop through second list
    while(temp!=NULL){
      
      //set another locator pointer to target list
      t = *s;
      
      //loop through target list
      while(t!=NULL){
      
        //if node of second list has same exponential, add coefficient to node of target list
        if(t->exp == temp->exp){
          t->coeff = t->coeff + temp->coeff;
          break;
        }
      
        //go to next node(target list)
        t = t->link;
      }
      
      //if reach end of target node(target list doesn't have the node of second list)
      if(t == NULL){
        //copy node to target list
        poly_append(s,temp->coeff,temp->exp);
      }
      
      //go to next node(second list)
      temp = temp->link;
    }
  }
  
}

//append node to linked list. add coefficient to the same exponential node.
void poly_append(struct polynode **q, float c, int e){
  struct polynode *r, *temp;
  
  //if list is empty, or the given exponential is bigger than the exponential of first node
  if(*q == NULL || e > (*q)->exp){
    //create new node
    r = malloc(sizeof(struct polynode));
    //set coefficient
    r->coeff = c;
    //set exponential
    r->exp = e;
    //set link of new node to point to the front node or null
    r->link = *q;
    //set pointer to point to the new node
    *q = r;
  }else if(e == (*q)->exp){ //if exponential value is the same to the front node
    //add coefficient to existing node
    (*q)->coeff = (*q)->coeff + c;
  }
  else{ //if list is not empty
    
    //set locator pointer to front of list
    temp = *q;
    
    //traverse until the last node
    while(temp->link!=NULL){
      //if given exponential is smaller than current exponential and bigger than next exponential
      if((temp->exp > e ) && (e > (temp->link)->exp )){
        //create new node
        r = malloc(sizeof(struct polynode));
        //set coefficient
        r->coeff = c;
        //set exponential
        r->exp = e;

        //set link of new node to point to next node
        r->link = temp->link;
        //set link of current node to point to new node
        temp->link = r;
        
        //go back to main
        return;
      }else if(e == temp->exp){//if given exponential is same to current node
        //add coefficient to existing node
        temp->coeff = temp->coeff + c;
        //go back to main
        return;
      }
      //go to next node
      temp = temp->link;
    }
    
    //if given exponential is same with last node
    if(e == temp->exp){
      //add coefficient to current node
      temp->coeff = temp->coeff + c;

      //go back to main
      return;
    }else{// no matching exponential node
      //create new node
      r = malloc(sizeof(struct polynode));
      //set coefficient
      r->coeff = c;
      //set exponential
      r->exp = e;
      //if last node is reached, set last node to point to new node
      temp->link = r;
      //set link of new node to null
      r->link = NULL;
    }
  }
}

//display linked list
void display_poly(struct polynode *q){
  //loop through linked list
  while(q!=NULL){
    //print bracket for negative coefficient
    if(q->coeff < 0){
      printf("(");
    }
    printf("%fx^%d",q->coeff,q->exp);
    //print bracket for negative coefficient
    if(q->coeff < 0){
      printf(")");
    }
    //if reach last node, don't print the plus sign
    if(q->link!=NULL){
      printf("+");
    }
    //go to next node
    q=q->link;
  }
  printf("\n");
}
```